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 #29

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
102 changes: 53 additions & 49 deletions dedupfs/dedupfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1025,25 +1025,26 @@ def __print_stats(self): # {{{3
self.__report_timings()

def __report_timings(self): # {{{3
if self.logger.isEnabledFor(logging.DEBUG):
timings = [(self.time_spent_traversing_tree, 'Traversing the tree'),
(self.time_spent_caching_nodes, 'Caching tree nodes'),
(self.time_spent_interning, 'Interning path components'),
(self.time_spent_writing_blocks, 'Writing data blocks'),
(self.time_spent_hashing, 'Hashing data blocks'),
(self.time_spent_querying_tree, 'Querying the tree')]
maxdescwidth = max([len(l) for t, l in timings]) + 3
timings.sort(reverse=True)
uptime = time.time() - self.fs_mounted_at
printed_heading = False
for timespan, description in timings:
percentage = timespan / (uptime / 100)
if percentage >= 1:
if not printed_heading:
self.logger.debug("Cumulative timings of slowest operations:")
printed_heading = True
self.logger.debug(
" - %-*s%s (%i%%)" % (maxdescwidth, description + ':', format_timespan(timespan), percentage))
if not self.logger.isEnabledFor(logging.DEBUG):
return
timings = [(self.time_spent_traversing_tree, 'Traversing the tree'),
(self.time_spent_caching_nodes, 'Caching tree nodes'),
(self.time_spent_interning, 'Interning path components'),
(self.time_spent_writing_blocks, 'Writing data blocks'),
(self.time_spent_hashing, 'Hashing data blocks'),
(self.time_spent_querying_tree, 'Querying the tree')]
maxdescwidth = max(len(l) for t, l in timings) + 3
timings.sort(reverse=True)
uptime = time.time() - self.fs_mounted_at
printed_heading = False
for timespan, description in timings:
percentage = timespan / (uptime / 100)
if percentage >= 1:
if not printed_heading:
self.logger.debug("Cumulative timings of slowest operations:")
printed_heading = True
self.logger.debug(
" - %-*s%s (%i%%)" % (maxdescwidth, description + ':', format_timespan(timespan), percentage))
Comment on lines -1028 to +1047
Copy link
Author

Choose a reason for hiding this comment

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

Function DedupFS.__report_timings refactored with the following changes:

  • Add guard clause (last-if-guard)
  • Replace unneeded comprehension with generator (comprehension-to-generator)


def report_disk_usage(self): # {{{3
disk_usage = self.__fetchval('PRAGMA page_size') * self.__fetchval('PRAGMA page_count')
Expand All @@ -1063,7 +1064,7 @@ def __report_memory_usage(self): # {{{3
self.memory_usage = memory_usage

def __report_throughput(self, nbytes=None, nseconds=None, label=None): # {{{3
if nbytes == None:
if nbytes is None:
Comment on lines -1066 to +1067
Copy link
Author

Choose a reason for hiding this comment

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

Function DedupFS.__report_throughput refactored with the following changes:

  • Use x is None rather than x == None (none-compare)

self.bytes_read, self.time_spent_reading = \
self.__report_throughput(self.bytes_read, self.time_spent_reading, "read")
self.bytes_written, self.time_spent_writing = \
Expand All @@ -1078,6 +1079,9 @@ def __report_throughput(self, nbytes=None, nseconds=None, label=None): # {{{3
return nbytes, nseconds

def __report_top_blocks(self): # {{{3
if not self.logger.isEnabledFor(logging.DEBUG):
return
printed_header = False
Comment on lines +1082 to +1084
Copy link
Author

Choose a reason for hiding this comment

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

Function DedupFS.__report_top_blocks refactored with the following changes:

  • Move assignments closer to their usage (move-assign)
  • Add guard clause (last-if-guard)

query = """
SELECT * FROM (
SELECT *, COUNT(*) AS "count" FROM "index"
Expand All @@ -1086,21 +1090,19 @@ def __report_top_blocks(self): # {{{3
"count" > 1 AND
hash_id = hashes.id
LIMIT 10 """
if self.logger.isEnabledFor(logging.DEBUG):
printed_header = False
for row in self.conn.execute(query):
if not printed_header:
self.logger.debug("A listing of the most used blocks follows:")
printed_header = True
msg = "Block #%s of %s has been used %i times: %r"
preview = row['value']
max_length = 60
if len(preview) < max_length:
preview = str(preview)
else:
preview = preview[0: max_length] + '...'
nbytes = format_size(len(row['value']))
self.logger.debug(msg, row['hash_id'], nbytes, row['count'], preview)
for row in self.conn.execute(query):
if not printed_header:
self.logger.debug("A listing of the most used blocks follows:")
printed_header = True
msg = "Block #%s of %s has been used %i times: %r"
preview = row['value']
max_length = 60
if len(preview) < max_length:
preview = str(preview)
else:
preview = preview[0: max_length] + '...'
nbytes = format_size(len(row['value']))
self.logger.debug(msg, row['hash_id'], nbytes, row['count'], preview)

def __gc_hook(self, nested=False): # {{{3
# Don't collect any garbage for nested calls.
Expand All @@ -1116,19 +1118,21 @@ def __gc_hook(self, nested=False): # {{{3
self.gc_hook_last_run = time.time()

def __collect_garbage(self): # {{{3
if self.gc_enabled and not self.read_only:
start_time = time.time()
self.logger.info("Performing garbage collection (this might take a while) ..")
self.should_vacuum = False
for method in self.__collect_strings, self.__collect_inodes, \
self.__collect_indices, self.__collect_blocks, self.__vacuum_metastore:
sub_start_time = time.time()
msg = method()
if msg:
elapsed_time = time.time() - sub_start_time
self.logger.info(msg, format_timespan(elapsed_time))
elapsed_time = time.time() - start_time
self.logger.info("Finished garbage collection in %s.", format_timespan(elapsed_time))
if not self.gc_enabled or self.read_only:
return

start_time = time.time()
self.logger.info("Performing garbage collection (this might take a while) ..")
self.should_vacuum = False
for method in self.__collect_strings, self.__collect_inodes, \
self.__collect_indices, self.__collect_blocks, self.__vacuum_metastore:
sub_start_time = time.time()
msg = method()
if msg:
elapsed_time = time.time() - sub_start_time
self.logger.info(msg, format_timespan(elapsed_time))
elapsed_time = time.time() - start_time
self.logger.info("Finished garbage collection in %s.", format_timespan(elapsed_time))
Comment on lines -1119 to +1135
Copy link
Author

Choose a reason for hiding this comment

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

Function DedupFS.__collect_garbage refactored with the following changes:

  • Add guard clause (last-if-guard)


def __collect_strings(self): # {{{4
count = self.conn.execute('DELETE FROM strings WHERE id NOT IN (SELECT name FROM tree)').rowcount
Expand All @@ -1150,7 +1154,7 @@ def __collect_indices(self): # {{{4

def __collect_blocks(self): # {{{4
should_reorganize = False
for row in self.conn.execute('SELECT hash FROM hashes WHERE id NOT IN (SELECT hash_id FROM "index")'):
for _ in self.conn.execute('SELECT hash FROM hashes WHERE id NOT IN (SELECT hash_id FROM "index")'):
Comment on lines -1153 to +1157
Copy link
Author

Choose a reason for hiding this comment

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

Function DedupFS.__collect_blocks refactored with the following changes:

  • Replace unused for index with underscore (for-index-underscore)

# del self.blocks[str(row[0])]
should_reorganize = True
if should_reorganize:
Expand Down
31 changes: 16 additions & 15 deletions dedupfs/fuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,12 @@ def get_compat_0_1():


# API version to be used
fuse_python_api = __getenv__('FUSE_PYTHON_API', '^[\d.]+$',
lambda x: tuple([int(i) for i in x.split('.')]))
fuse_python_api = __getenv__(
'FUSE_PYTHON_API',
'^[\d.]+$',
lambda x: tuple(int(i) for i in x.split('.')),
)

Comment on lines -76 to +81
Copy link
Author

Choose a reason for hiding this comment

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

Lines 76-77 refactored with the following changes:

  • Replace unneeded comprehension with generator (comprehension-to-generator)


# deprecated way of API specification
compat_0_1 = __getenv__('FUSE_PYTHON_COMPAT', '^(0.1|ALL)$', lambda x: True)
Expand Down Expand Up @@ -126,9 +130,7 @@ def unsetmod(self, mod):
def mount_expected(self):
if self.getmod('showhelp'):
return False
if self.getmod('showversion'):
return False
return True
return not self.getmod('showversion')
Comment on lines -129 to +133
Copy link
Author

Choose a reason for hiding this comment

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

Function FuseArgs.mount_expected refactored with the following changes:

  • Simplify conditional into return statement (return-identity)


def assemble(self):
"""Mangle self into an argument array"""
Expand All @@ -141,9 +143,7 @@ def assemble(self):
if v:
args.append(self.fuse_modifiers[m])

opta = []
for o, v in self.optdict.iteritems():
opta.append(o + '=' + v)
opta = [o + '=' + v for o, v in self.optdict.iteritems()]
Comment on lines -144 to +146
Copy link
Author

Choose a reason for hiding this comment

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

Function FuseArgs.assemble refactored with the following changes:

  • Convert for loop into list comprehension (list-comprehension)

opta.extend(self.optlist)

if opta:
Expand All @@ -167,7 +167,7 @@ def filter(self, other=None):
class FuseFormatter(SubbedOptIndentedFormatter):

def __init__(self, **kw):
if not 'indent_increment' in kw:
if 'indent_increment' not in kw:
Copy link
Author

Choose a reason for hiding this comment

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

Function FuseFormatter.__init__ refactored with the following changes:

  • Simplify logical expression using De Morgan identities (de-morgan)

kw['indent_increment'] = 4
SubbedOptIndentedFormatter.__init__(self, **kw)

Expand Down Expand Up @@ -338,7 +338,7 @@ def add_option(self, *opts, **attrs):
"having options or specifying the `subopt' attribute conflicts with `mountopt' attribute")
opts = ('-o',)
attrs['subopt'] = attrs.pop('mountopt')
if not 'dest' in attrs:
if 'dest' not in attrs:
Copy link
Author

Choose a reason for hiding this comment

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

Function FuseOptParse.add_option refactored with the following changes:

  • Simplify logical expression using De Morgan identities (de-morgan)

attrs['dest'] = attrs['subopt']

SubbedOptParse.add_option(self, *opts, **attrs)
Expand Down Expand Up @@ -559,8 +559,7 @@ def resolve(args, maxva):
maxva[0] = max(maxva[0], fp)
continue
if isinstance(fp, list) or isinstance(fp, tuple):
for f in fp:
yield f
yield from fp
Copy link
Author

Choose a reason for hiding this comment

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

Function feature_needs.resolve refactored with the following changes:

  • Replace yield inside for loop with yield from (yield-from)
  • Simplify logical expression using De Morgan identities (de-morgan)

continue
ma = isinstance(fp, str) and re.compile("(!\s*|)re:(.*)").match(fp)
if isinstance(fp, type(re.compile(''))) or ma:
Expand All @@ -574,7 +573,7 @@ def resolve(args, maxva):
yield f
continue
ma = re.compile("has_(.*)").match(fp)
if ma and ma.groups()[0] in Fuse._attrs and not fp in fmap:
if ma and ma.groups()[0] in Fuse._attrs and fp not in fmap:
yield 21
continue
yield fmap[fp]
Expand Down Expand Up @@ -728,8 +727,10 @@ def main(self, args=None):
if get_compat_0_1():
args = self.main_0_1_preamble()

d = {'multithreaded': self.multithreaded and 1 or 0}
d['fuse_args'] = args or self.fuse_args.assemble()
d = {
'multithreaded': self.multithreaded and 1 or 0,
'fuse_args': args or self.fuse_args.assemble(),
}
Comment on lines -731 to +733
Copy link
Author

Choose a reason for hiding this comment

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

Function Fuse.main refactored with the following changes:

  • Merge dictionary assignment with declaration (merge-dict-assign)


for t in 'file_class', 'dir_class':
if hasattr(self, t):
Expand Down
4 changes: 2 additions & 2 deletions download_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def download_block(hash_uid, filename):

entity = client.get_entity(client.get_me())
messages = client.get_messages(entity, limit=1, search=hash_uid)
for i in range(len(messages)):
msg = messages[i]
for message in messages:
msg = message
Comment on lines -56 to +57
Copy link
Author

Choose a reason for hiding this comment

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

Function download_block refactored with the following changes:

  • Replace index in for loop with direct reference (for-index-replacement)

if msg.message == hash_uid:
# FIFO = f"dpipe_{hash_uid}"
# import errno
Expand Down
6 changes: 1 addition & 5 deletions telegram_client_x.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,7 @@ def upload_file(self,
# Set a default file name if None was specified
file_id = helpers.generate_random_long()
if not file_name:
if isinstance(file, str):
file_name = os.path.basename(file)
else:
file_name = str(file_id)

file_name = os.path.basename(file) if isinstance(file, str) else str(file_id)
Copy link
Author

Choose a reason for hiding this comment

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

Function TelegramClientX.upload_file refactored with the following changes:

  • Swap positions of nested conditionals (swap-nested-ifs)
  • Hoist repeated code outside conditional statement (hoist-statement-from-if)
  • Replace if statement with if expression (assign-if-exp)

# Determine whether the file is too big (over 10MB) or not
# Telegram does make a distinction between smaller or larger files
is_large = file_size > 10 * 1024 * 1024
Expand Down