From 3c79961f0f833f5a73df0fd8c87e751c852bdd9b Mon Sep 17 00:00:00 2001 From: brennerm Date: Thu, 19 Mar 2015 11:50:11 +0100 Subject: [PATCH 01/11] Fixed README formatting --- README.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 64c68f6..bb3452f 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,10 @@ Creating a knowledge base of unpopular Python built-in features to save a lot of ## Contribute Feel free to use the PyTrickBase.txt as a starting point. -1. Pull request -Send a pull request with your PyTrick, containing example code and a documentation one-liner. Be sure to add yourself to the contributers. +1. Pull request: -2. Issue comment -Add your Python snippet and your documentation as a comment on Issue#1. I will take care of adding your PyTrick and you as a contributer. + Send a pull request with your PyTrick, containing example code and a documentation one-liner. Be sure to add yourself to the contributers. + +2. Issue comment: + + Add your Python snippet and your documentation as a comment on Issue#1. I will take care of adding your PyTrick and you as a contributer. From 16fe081e8248d31730ce278e1e59a4f049b8f32a Mon Sep 17 00:00:00 2001 From: brennerm Date: Thu, 19 Mar 2015 14:25:45 +0100 Subject: [PATCH 02/11] added contribution of obeleh --- CONTRIBUTERS | 1 + loopoverlappingdicts.py | 8 ++++++++ 2 files changed, 9 insertions(+) create mode 100755 loopoverlappingdicts.py diff --git a/CONTRIBUTERS b/CONTRIBUTERS index 27f9ec5..c1b2648 100644 --- a/CONTRIBUTERS +++ b/CONTRIBUTERS @@ -1,2 +1,3 @@ - brennerm - agumonkey +- obeleh diff --git a/loopoverlappingdicts.py b/loopoverlappingdicts.py new file mode 100755 index 0000000..ec9d93c --- /dev/null +++ b/loopoverlappingdicts.py @@ -0,0 +1,8 @@ +#! /usr/bin/env python3 +"""loop over dicts that share (some) keys""" + +dctA = {'a': 1, 'b': 2, 'c': 3} +dctB = {'b': 4, 'c': 5, 'd': 6} + +for ky in set(dctA) & set(dctB): + print(ky) From db1c1d9963654dc3b0e7a0568e3a8467b244f1f2 Mon Sep 17 00:00:00 2001 From: brennerm Date: Thu, 19 Mar 2015 14:57:21 +0100 Subject: [PATCH 03/11] added contribution of obeleh --- copylist.py | 5 +++++ 1 file changed, 5 insertions(+) create mode 100755 copylist.py diff --git a/copylist.py b/copylist.py new file mode 100755 index 0000000..031048a --- /dev/null +++ b/copylist.py @@ -0,0 +1,5 @@ +#! /usr/bin/env python3 +"""a fast way to copy a list""" + +a = [1, 2, 3, 4, 5] +print(a[:]) From 358f8b2003a86ca1d8b5bd18d9fa527d15f4e175 Mon Sep 17 00:00:00 2001 From: Serdar Yegulalp Date: Thu, 19 Mar 2015 11:59:17 -0400 Subject: [PATCH 04/11] Update namedformatting.py Suggested implementation for .format --- namedformatting.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/namedformatting.py b/namedformatting.py index 7924089..cc71077 100755 --- a/namedformatting.py +++ b/namedformatting.py @@ -2,4 +2,9 @@ """easy string formatting using dicts""" d = {'name': 'Jeff', 'age': 24} -print("My name is %(name)s and I'm %(age)i years old." % d) \ No newline at end of file +print("My name is %(name)s and I'm %(age)i years old." % d) + +"""for .format, use this method""" + +d = {'name': 'Jeff', 'age': 24} +print("My name is {name} and I'm {age} years old.".format(**d)) From 70a41d081cd27aa7456490f44d50a567783962d7 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Fri, 20 Mar 2015 05:53:30 -0400 Subject: [PATCH 05/11] Corrected spelling Changed file name CONTRIBUTERS -> CONTRIBUTORS, corrected spelling of "contributor" in README.md --- CONTRIBUTERS => CONTRIBUTORS | 0 README.md | 4 ++-- 2 files changed, 2 insertions(+), 2 deletions(-) rename CONTRIBUTERS => CONTRIBUTORS (100%) diff --git a/CONTRIBUTERS b/CONTRIBUTORS similarity index 100% rename from CONTRIBUTERS rename to CONTRIBUTORS diff --git a/README.md b/README.md index bb3452f..494012e 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,8 @@ Feel free to use the PyTrickBase.txt as a starting point. 1. Pull request: - Send a pull request with your PyTrick, containing example code and a documentation one-liner. Be sure to add yourself to the contributers. + Send a pull request with your PyTrick, containing example code and a documentation one-liner. Be sure to add yourself to the contributors. 2. Issue comment: - Add your Python snippet and your documentation as a comment on Issue#1. I will take care of adding your PyTrick and you as a contributer. + Add your Python snippet and your documentation as a comment on Issue#1. I will take care of adding your PyTrick and you as a contributor. From beb1ba30f7038f63f4a1513a87c7068bb85de613 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Fri, 20 Mar 2015 06:04:49 -0400 Subject: [PATCH 06/11] added concatenatestrings --- CONTRIBUTORS | 1 + concatenatestrings.py | 6 ++++++ 2 files changed, 7 insertions(+) create mode 100644 concatenatestrings.py diff --git a/CONTRIBUTORS b/CONTRIBUTORS index c1b2648..bddf46e 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -1,3 +1,4 @@ - brennerm - agumonkey - obeleh +- prooffreader diff --git a/concatenatestrings.py b/concatenatestrings.py new file mode 100644 index 0000000..765bd72 --- /dev/null +++ b/concatenatestrings.py @@ -0,0 +1,6 @@ +#! /usr/bin/env python3 +"""Concatenate strings to elegantly go over line breaks in code""" + +my_long_text = ("We are no longer the knights who say Ni! " + "We are now the knights who say ekki-ekki-" + "ekki-p'tang-zoom-boing-z'nourrwringmm!") \ No newline at end of file From 735c909f1f3cdf4dae9f048014dd6a6a68d0ddf3 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Fri, 20 Mar 2015 06:06:41 -0400 Subject: [PATCH 07/11] Revert "added concatenatestrings" This reverts commit beb1ba30f7038f63f4a1513a87c7068bb85de613. --- CONTRIBUTORS | 1 - concatenatestrings.py | 6 ------ 2 files changed, 7 deletions(-) delete mode 100644 concatenatestrings.py diff --git a/CONTRIBUTORS b/CONTRIBUTORS index bddf46e..c1b2648 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -1,4 +1,3 @@ - brennerm - agumonkey - obeleh -- prooffreader diff --git a/concatenatestrings.py b/concatenatestrings.py deleted file mode 100644 index 765bd72..0000000 --- a/concatenatestrings.py +++ /dev/null @@ -1,6 +0,0 @@ -#! /usr/bin/env python3 -"""Concatenate strings to elegantly go over line breaks in code""" - -my_long_text = ("We are no longer the knights who say Ni! " - "We are now the knights who say ekki-ekki-" - "ekki-p'tang-zoom-boing-z'nourrwringmm!") \ No newline at end of file From 836ccc4c64567b9d926377a81a70c273136a088b Mon Sep 17 00:00:00 2001 From: David Taylor Date: Fri, 20 Mar 2015 06:10:05 -0400 Subject: [PATCH 08/11] added concatenatestrings.py --- CONTRIBUTORS | 1 + concatenatestrings.py | 7 +++++++ 2 files changed, 8 insertions(+) create mode 100644 concatenatestrings.py diff --git a/CONTRIBUTORS b/CONTRIBUTORS index c1b2648..21ef6ae 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -1,3 +1,4 @@ - brennerm - agumonkey - obeleh +- Prooffreader \ No newline at end of file diff --git a/concatenatestrings.py b/concatenatestrings.py new file mode 100644 index 0000000..fac8d55 --- /dev/null +++ b/concatenatestrings.py @@ -0,0 +1,7 @@ +#! /usr/bin/env python3 +"""Concatenate long strings elegantly +across line breaks in code""" + +my_long_text = ("We are no longer the knights who say Ni! " + "We are now the knights who say ekki-ekki-" + "ekki-p'tang-zoom-boing-z'nourrwringmm!") From e9bf844c7126a8a2ff65f9411b6b5f8e07711c27 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Fri, 20 Mar 2015 06:24:21 -0400 Subject: [PATCH 09/11] added reversedict.py --- reversedict.py | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 reversedict.py diff --git a/reversedict.py b/reversedict.py new file mode 100644 index 0000000..5d4dfb3 --- /dev/null +++ b/reversedict.py @@ -0,0 +1,7 @@ +#! /usr/bin/env python3 +"""Reverses keys and values in a dict""" + +_dict = {"one": 1, "two": 2} +# make sure dict is reversible +assert len(set(_dict.keys())) == len(set(_dict.values())) +reversed_dict = {v: k for k, v in _dict.items()} \ No newline at end of file From ae679ee20add2192c6c3fa95e00e395313ed8bc2 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Fri, 20 Mar 2015 06:35:20 -0400 Subject: [PATCH 10/11] changed to better name --- reversedict.py => dictswapkeysvalues.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename reversedict.py => dictswapkeysvalues.py (100%) diff --git a/reversedict.py b/dictswapkeysvalues.py similarity index 100% rename from reversedict.py rename to dictswapkeysvalues.py From 924e359dba3f8ac9f3fc038c13b022237fb75ef0 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Fri, 20 Mar 2015 07:35:07 -0400 Subject: [PATCH 11/11] better language and shorter code in dictswapkeysvalues.py --- dictswapkeysvalues.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dictswapkeysvalues.py b/dictswapkeysvalues.py index 5d4dfb3..73d2e9d 100644 --- a/dictswapkeysvalues.py +++ b/dictswapkeysvalues.py @@ -1,7 +1,7 @@ #! /usr/bin/env python3 -"""Reverses keys and values in a dict""" +"""Swaps keys and values in a dict""" _dict = {"one": 1, "two": 2} -# make sure dict is reversible -assert len(set(_dict.keys())) == len(set(_dict.values())) +# make sure all of dict's values are unique +assert len(_dict) == len(set(_dict.values())) reversed_dict = {v: k for k, v in _dict.items()} \ No newline at end of file