Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sourcery refactored master branch #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 13 additions & 18 deletions misc/log-analytics/import_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def check_format(self, file):
file.seek(0)
return
# Skip the next 2 lines.
for i in xrange(2):
for _ in xrange(2):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function IisFormat.check_format refactored with the following changes:

file.readline()
# Parse the 4th line (regex)
full_regex = []
Expand Down Expand Up @@ -668,7 +668,7 @@ def increment(self):
self.value = self.counter.next()

def advance(self, n):
for i in range(n):
for _ in range(n):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Statistics.Counter.advance refactored with the following changes:

self.increment()

def __str__(self):
Expand Down Expand Up @@ -718,10 +718,7 @@ def _compute_speed(self, value, start, end):
delta_time = end - start
if value == 0:
return 0
if delta_time == 0:
return 'very high!'
else:
return value / delta_time
return 'very high!' if delta_time == 0 else value / delta_time
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Statistics._compute_speed refactored with the following changes:


def _round_value(self, value, base=100):
return round(value * base) / base
Expand Down Expand Up @@ -1034,9 +1031,7 @@ def _add_site(self, hit):
DynamicResolver._add_site_lock.acquire()

try:
# After we obtain the lock, make sure the site hasn't already been created.
res = self._get_site_id_from_hit_host(hit)
if res:
if res := self._get_site_id_from_hit_host(hit):
Comment on lines -1037 to +1034
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function DynamicResolver._add_site refactored with the following changes:

This removes the following comments ( why? ):

# After we obtain the lock, make sure the site hasn't already been created.

return res[0]['idsite']

# The site doesn't exist.
Expand Down Expand Up @@ -1071,8 +1066,7 @@ def _add_site(self, hit):
DynamicResolver._add_site_lock.release()

def _resolve(self, hit):
res = self._get_site_id_from_hit_host(hit)
if res:
if res := self._get_site_id_from_hit_host(hit):
Comment on lines -1074 to +1069
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function DynamicResolver._resolve refactored with the following changes:

# The site already exists.
site_id = res[0]['idsite']
else:
Expand Down Expand Up @@ -1152,7 +1146,7 @@ def launch(cls, recorder_count):
"""
Launch a bunch of Recorder objects in a separate thread.
"""
for i in xrange(recorder_count):
for _ in xrange(recorder_count):
Comment on lines -1155 to +1149
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Recorder.launch refactored with the following changes:

recorder = Recorder()
cls.recorders.append(recorder)

Expand All @@ -1170,7 +1164,7 @@ def add_hits(cls, all_hits):
"""
# Organize hits so that one client IP will always use the same queue.
# We have to do this so visits from the same IP will be added in the right order.
hits_by_client = [[] for r in cls.recorders]
hits_by_client = [[] for _ in cls.recorders]
Comment on lines -1173 to +1167
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Recorder.add_hits refactored with the following changes:

for hit in all_hits:
hits_by_client[abs(hash(hit.ip)) % len(cls.recorders)].append(hit)

Expand Down Expand Up @@ -1446,10 +1440,11 @@ def check_path(self, hit):
return False
# By default, all paths are included.
if config.options.included_paths:
for included_path in config.options.included_paths:
if fnmatch.fnmatch(hit.path, included_path):
return True
return False
return any(
fnmatch.fnmatch(hit.path, included_path)
for included_path in config.options.included_paths
)

Comment on lines -1449 to +1447
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Parser.check_path refactored with the following changes:

  • Use any() instead of for loop (use-any)

return True

@staticmethod
Expand Down Expand Up @@ -1503,7 +1498,7 @@ def detect_format(file):
limit = 100000
while not format and lineno < limit:
line = file.readline()
lineno = lineno + 1
lineno += 1
Comment on lines -1506 to +1501
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Parser.detect_format refactored with the following changes:

  • Replace assignment with augmented assignment (aug-assign)


logging.debug("Detecting format against line %i" % lineno)
format = Parser.check_format(line)
Expand Down
20 changes: 7 additions & 13 deletions misc/log-analytics/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@

# utility functions
def add_junk_to_file(path):
file = open(path)
contents = file.read()
file.close()

file = open('tmp.log', 'w')
file.write(contents + ' junk')
file.close()

with open(path) as file:
contents = file.read()
with open('tmp.log', 'w') as file:
file.write(contents + ' junk')
Comment on lines -9 to +12
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function add_junk_to_file refactored with the following changes:

return 'tmp.log'

def tearDownModule():
Expand Down Expand Up @@ -185,11 +181,9 @@ def test_replay_tracking_arguments():

def parse_log_file_line(format_name, file_):
format = import_logs.FORMATS[format_name]

file = open(file_)
match = format.check_format(file)
file.close()


with open(file_) as file:
match = format.check_format(file)
Comment on lines -188 to +186
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function parse_log_file_line refactored with the following changes:

return format.get_all()

# check parsing groups
Expand Down