-
Notifications
You must be signed in to change notification settings - Fork 11
/
welcome-to-sourcery.py
136 lines (99 loc) · 4.89 KB
/
welcome-to-sourcery.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# Welcome to Sourcery! We're here to be your pair programmer anytime you're
# working in VS Code.
# To get started log into your Sourcery account. Click on the Sourcery logo
# (the hexagon) on your VS Code sidebar and click the login button, or open
# the command palette (Ctrl/Cmd+Shift+P) and execute `Sourcery: Login`.
# Let's start looking at how you can get a code review from Sourcery.
# The `Review` tab allows you to get a code review straight away in your IDE
# - You can always get a review of the current file
# - If in Git you can review your current set of uncommitted changes,
# or your current branch compared to the default branch
# If you want reviews when you open a PR, you can add Sourcery to your GitHub or GitLab repos.
# Now let's move on to the `Chat` tab.
# Above each function you'll see a few commands - these are Code Lenses that
# you can use to interact with Sourcery. Try clicking on "Ask Sourcery" and
# asking it to update the code to use `dateutil`. The answer will appear in
# the Sourcery sidebar chat.
def days_between_dates(date1, date2):
d1 = datetime.datetime.strptime(date1, '%Y-%m-%d').date()
d2 = datetime.datetime.strptime(date2, '%Y-%m-%d').date()
delta = d2 - d1
return delta.days
# With the Ask Sourcery command or the chat in the sidebar you can ask Sourcery
# questions, have it write new code for you, or update existing code.
# Sourcery also has a series of "recipes" to do different things with code.
# Try clicking the Generate Docstrings lens above this next function:
def calculate_weighted_moving_average(prices, weights):
if not prices or not weights:
raise ValueError("Both prices and weights must be provided.")
if len(weights) > len(prices):
raise ValueError("Length of weights must be less than or equal to length of prices.")
total_weight = sum(weights)
normalized_weights = [w / total_weight for w in weights]
wma = []
for i in range(len(prices) - len(weights) + 1):
weighted_sum = sum(prices[i + j] * normalized_weights[j] for j in range(len(weights)))
wma.append(weighted_sum)
return wma
# Now try clicking Generate Tests or Explain Code for the same function!
# There are also recipes for Optimizing Performance and Simplifying Code.
# You can access these by clicking Ask Sourcery and choosing them from the
# dropdown or by selecting a section of code and clicking the recipe button
# in the sidebar.
# In your code you'll also see sections start to get underlined.
# This means Sourcery has a suggestion to improve it.
def refactoring_example(spellbook):
result = []
for spell in spellbook:
if spell.is_awesome:
result.append(spell)
print(result)
# Hover over the underlined code to see details of the changes including a diff.
# You can accept Sourcery's changes with the quick fix action. Put your cursor
# on the highlighted line and click on the lightbulb.
#
# Or use the quick-fix hotkey (Ctrl .) or (Cmd .) and then choose
# "Sourcery - Convert for loop...". This will instantly replace the code with
# the improved version.
# The Problems pane (Ctrl/Cmd+Shift+M) shows all of Sourcery's suggestions.
# Sourcery also provides code metrics for each function to give you insight into
# code quality - hover over the function definition below to see this report.
def magical_hoist(magic):
if is_powerful(magic):
result = 'Magic'
else:
print("Not powerful.")
result = 'Magic'
print(result)
# What if we don't want to make the change Sourcery suggests?
# You can skip/ignore changes from Sourcery in a few ways:
# 1) In the quick fix menu choose "Sourcery - Skip suggested refactoring"
# This adds a comment to the function telling Sourcery not to make the change.
# 2) In the quick fix menu choose "Sourcery - Never show me this refactoring"
# This tells Sourcery to never suggest this type of suggestion. This config
# is stored in a configuration file on your machine.
# 3) Click on the Sourcery button in the Status Bar (typically the bottom of
# the VS Code window) to bring up the Sourcery Hub. Click on "Settings" and
# then you can toggle individual rule types on or off
# For more details check out our documentation here:
# https://docs.sourcery.ai/
# If you want to play around a bit more, here are some more examples of Sourcery's in-line suggestions.
# These include cases where Sourcery has chained together suggestions to come
# up with more powerful refactorings.
def find_more(magicks):
powerful_magic = []
for magic in magicks:
if not is_powerful(magic):
continue
powerful_magic.append(magic)
return powerful_magic
def is_powerful(magic):
if magic == 'Sourcery':
return True
elif magic == 'More Sourcery':
return True
else:
return False
def print_all(spells: list):
for i in range(len(spells)):
print(spells[i])