-
Notifications
You must be signed in to change notification settings - Fork 0
Sourcery refactored main branch #1
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,14 +13,7 @@ class DBPickler(pickle.Pickler): | |
| def persistent_id(self, obj): | ||
| # Instead of pickling MemoRecord as a regular class instance, we emit a | ||
| # persistent ID. | ||
| if isinstance(obj, MemoRecord): | ||
| # Here, our persistent ID is simply a tuple, containing a tag and a | ||
| # key, which refers to a specific record in the database. | ||
| return ("MemoRecord", obj.key) | ||
| else: | ||
| # If obj does not have a persistent ID, return None. This means obj | ||
| # needs to be pickled as usual. | ||
| return None | ||
| return ("MemoRecord", obj.key) if isinstance(obj, MemoRecord) else None | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ): |
||
|
|
||
|
|
||
| class DBUnpickler(pickle.Unpickler): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,10 +16,10 @@ | |
| 'Body would go here\n') | ||
|
|
||
| # Now the header items can be accessed as a dictionary: | ||
| print('To: {}'.format(headers['to'])) | ||
| print('From: {}'.format(headers['from'])) | ||
| print('Subject: {}'.format(headers['subject'])) | ||
| print(f"To: {headers['to']}") | ||
| print(f"From: {headers['from']}") | ||
| print(f"Subject: {headers['subject']}") | ||
|
|
||
| # You can also access the parts of the addresses: | ||
| print('Recipient username: {}'.format(headers['to'].addresses[0].username)) | ||
| print('Sender name: {}'.format(headers['from'].addresses[0].display_name)) | ||
| print(f"Recipient username: {headers['to'].addresses[0].username}") | ||
| print(f"Sender name: {headers['from'].addresses[0].display_name}") | ||
|
Comment on lines
-19
to
+25
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,31 +43,35 @@ def magic_html_parser(html_text, partfiles): | |
| # We can extract the richest alternative in order to display it: | ||
| richest = msg.get_body() | ||
| partfiles = {} | ||
| if richest['content-type'].maintype == 'text': | ||
| if richest['content-type'].subtype == 'plain': | ||
| for line in richest.get_content().splitlines(): | ||
| print(line) | ||
| sys.exit() | ||
| elif richest['content-type'].subtype == 'html': | ||
| body = richest | ||
| else: | ||
| print("Don't know how to display {}".format(richest.get_content_type())) | ||
| sys.exit() | ||
| elif richest['content-type'].content_type == 'multipart/related': | ||
| if ( | ||
| richest['content-type'].maintype == 'text' | ||
| and richest['content-type'].subtype == 'plain' | ||
| ): | ||
| for line in richest.get_content().splitlines(): | ||
| print(line) | ||
| sys.exit() | ||
| elif ( | ||
| richest['content-type'].maintype == 'text' | ||
| and richest['content-type'].subtype == 'html' | ||
| ): | ||
| body = richest | ||
| elif ( | ||
| richest['content-type'].maintype == 'text' | ||
| or richest['content-type'].content_type != 'multipart/related' | ||
| ): | ||
| print(f"Don't know how to display {richest.get_content_type()}") | ||
| sys.exit() | ||
| else: | ||
| body = richest.get_body(preferencelist=('html')) | ||
| for part in richest.iter_attachments(): | ||
| fn = part.get_filename() | ||
| if fn: | ||
| if fn := part.get_filename(): | ||
|
Comment on lines
-46
to
+67
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
| extension = os.path.splitext(part.get_filename())[1] | ||
| else: | ||
| extension = mimetypes.guess_extension(part.get_content_type()) | ||
| with tempfile.NamedTemporaryFile(suffix=extension, delete=False) as f: | ||
| f.write(part.get_content()) | ||
| # again strip the <> to go from email form of cid to html form. | ||
| partfiles[part['content-id'][1:-1]] = f.name | ||
| else: | ||
| print("Don't know how to display {}".format(richest.get_content_type())) | ||
| sys.exit() | ||
| with tempfile.NamedTemporaryFile(mode='w', delete=False) as f: | ||
| f.write(magic_html_parser(body.get_content(), partfiles)) | ||
| webbrowser.open(f.name) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,10 +19,7 @@ | |
| dom = xml.dom.minidom.parseString(document) | ||
|
|
||
| def getText(nodelist): | ||
| rc = [] | ||
| for node in nodelist: | ||
| if node.nodeType == node.TEXT_NODE: | ||
| rc.append(node.data) | ||
| rc = [node.data for node in nodelist if node.nodeType == node.TEXT_NODE] | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| return ''.join(rc) | ||
|
|
||
| def handleSlideshow(slideshow): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,10 +9,7 @@ | |
|
|
||
| def calculate(func, args): | ||
| result = func(*args) | ||
| return '%s says that %s%s = %s' % ( | ||
| multiprocessing.current_process().name, | ||
| func.__name__, args, result | ||
| ) | ||
| return f'{multiprocessing.current_process().name} says that {func.__name__}{args} = {result}' | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| def calculatestar(args): | ||
| return calculate(*args) | ||
|
|
@@ -106,8 +103,7 @@ def test(): | |
| try: | ||
| x = next(it) | ||
| except ZeroDivisionError: | ||
| if i == 5: | ||
| pass | ||
| pass | ||
|
Comment on lines
-109
to
+106
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| except StopIteration: | ||
| break | ||
| else: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,8 +18,7 @@ def worker(input, output): | |
|
|
||
| def calculate(func, args): | ||
| result = func(*args) | ||
| return '%s says that %s%s = %s' % \ | ||
| (current_process().name, func.__name__, args, result) | ||
| return f'{current_process().name} says that {func.__name__}{args} = {result}' | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| # | ||
| # Functions referenced by tasks | ||
|
|
@@ -51,24 +50,24 @@ def test(): | |
| task_queue.put(task) | ||
|
|
||
| # Start worker processes | ||
| for i in range(NUMBER_OF_PROCESSES): | ||
| for _ in range(NUMBER_OF_PROCESSES): | ||
| Process(target=worker, args=(task_queue, done_queue)).start() | ||
|
|
||
| # Get and print results | ||
| print('Unordered results:') | ||
| for i in range(len(TASKS1)): | ||
| for _ in TASKS1: | ||
|
Comment on lines
-54
to
+58
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| print('\t', done_queue.get()) | ||
|
|
||
| # Add more tasks using `put()` | ||
| for task in TASKS2: | ||
| task_queue.put(task) | ||
|
|
||
| # Get and print some more results | ||
| for i in range(len(TASKS2)): | ||
| for _ in range(len(TASKS2)): | ||
| print('\t', done_queue.get()) | ||
|
|
||
| # Tell child processes to stop | ||
| for i in range(NUMBER_OF_PROCESSES): | ||
| for _ in range(NUMBER_OF_PROCESSES): | ||
| task_queue.put('STOP') | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,7 +49,7 @@ def fopen(fname): | |
| try: | ||
| return open(fname) | ||
| except IOError as detail: | ||
| return fail("couldn't open " + fname + ": " + str(detail)) | ||
| return fail(f"couldn't open {fname}: {str(detail)}") | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| # open two files & spray the diff to stdout; return false iff a problem | ||
| def fcompare(f1name, f2name): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,11 +11,7 @@ | |
| import time as _time | ||
|
|
||
| STDOFFSET = timedelta(seconds = -_time.timezone) | ||
| if _time.daylight: | ||
| DSTOFFSET = timedelta(seconds = -_time.altzone) | ||
| else: | ||
| DSTOFFSET = STDOFFSET | ||
|
|
||
| DSTOFFSET = timedelta(seconds=-_time.altzone) if _time.daylight else STDOFFSET | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
| DSTDIFF = DSTOFFSET - STDOFFSET | ||
|
|
||
| class LocalTimezone(tzinfo): | ||
|
|
@@ -31,16 +27,10 @@ def fromutc(self, dt): | |
| tzinfo=self, fold=fold) | ||
|
|
||
| def utcoffset(self, dt): | ||
| if self._isdst(dt): | ||
| return DSTOFFSET | ||
| else: | ||
| return STDOFFSET | ||
| return DSTOFFSET if self._isdst(dt) else STDOFFSET | ||
|
Comment on lines
-34
to
+30
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| def dst(self, dt): | ||
| if self._isdst(dt): | ||
| return DSTDIFF | ||
| else: | ||
| return ZERO | ||
| return DSTDIFF if self._isdst(dt) else ZERO | ||
|
Comment on lines
-40
to
+33
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| def tzname(self, dt): | ||
| return _time.tzname[self._isdst(dt)] | ||
|
|
@@ -59,8 +49,7 @@ def _isdst(self, dt): | |
| # A complete implementation of current DST rules for major US time zones. | ||
|
|
||
| def first_sunday_on_or_after(dt): | ||
| days_to_go = 6 - dt.weekday() | ||
| if days_to_go: | ||
| if days_to_go := 6 - dt.weekday(): | ||
|
Comment on lines
-62
to
+52
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| dt += timedelta(days_to_go) | ||
| return dt | ||
|
|
||
|
|
@@ -93,7 +82,7 @@ def first_sunday_on_or_after(dt): | |
| def us_dst_range(year): | ||
| # Find start and end times for US DST. For years before 1967, return | ||
| # start = end for no DST. | ||
| if 2006 < year: | ||
| if year > 2006: | ||
|
Comment on lines
-96
to
+85
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| dststart, dstend = DSTSTART_2007, DSTEND_2007 | ||
| elif 1986 < year < 2007: | ||
| dststart, dstend = DSTSTART_1987_2006, DSTEND_1987_2006 | ||
|
|
@@ -119,10 +108,7 @@ def __repr__(self): | |
| return self.reprname | ||
|
|
||
| def tzname(self, dt): | ||
| if self.dst(dt): | ||
| return self.dstname | ||
| else: | ||
| return self.stdname | ||
| return self.dstname if self.dst(dt) else self.stdname | ||
|
Comment on lines
-122
to
+111
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| def utcoffset(self, dt): | ||
| return self.stdoffset + self.dst(dt) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lines
52-378refactored with the following changes:replace-interpolation-with-fstring)use-fstring-for-concatenation)merge-dict-assign)This removes the following comments ( why? ):