From 8abd0e4f2ac1580a64039279faa7266c5797807c Mon Sep 17 00:00:00 2001 From: Abdul Moiz <33980210+progrmoiz@users.noreply.github.com> Date: Sun, 10 Dec 2017 20:43:23 +0500 Subject: [PATCH 01/22] Delete LICENSE --- LICENSE | 24 ------------------------ 1 file changed, 24 deletions(-) delete mode 100644 LICENSE diff --git a/LICENSE b/LICENSE deleted file mode 100644 index cf1ab25..0000000 --- a/LICENSE +++ /dev/null @@ -1,24 +0,0 @@ -This is free and unencumbered software released into the public domain. - -Anyone is free to copy, modify, publish, use, compile, sell, or -distribute this software, either in source code form or as a compiled -binary, for any purpose, commercial or non-commercial, and by any -means. - -In jurisdictions that recognize copyright laws, the author or authors -of this software dedicate any and all copyright interest in the -software to the public domain. We make this dedication for the benefit -of the public at large and to the detriment of our heirs and -successors. We intend this dedication to be an overt act of -relinquishment in perpetuity of all present and future rights to this -software under copyright law. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR -OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -OTHER DEALINGS IN THE SOFTWARE. - -For more information, please refer to From c80c16d1dcd3c854b3d5b3e65f7f404495844748 Mon Sep 17 00:00:00 2001 From: Moiz Date: Mon, 11 Dec 2017 21:43:40 +0500 Subject: [PATCH 02/22] added listtree.py --- listtree.py | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 listtree.py diff --git a/listtree.py b/listtree.py new file mode 100644 index 0000000..b1874b4 --- /dev/null +++ b/listtree.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python3 +# File listtree.py (2.x + 3.x) + + +class ListTree: + """ + Mix-in that returns an __str__ trace of the entire class and all + its object's attrs at and above self; run by print(), str() returns + constructed string; uses __X attr names to avoid impacting clients; + recurses to superclasses explicitly, uses str.format() for clarity. + """ + + def __attrnames(self, obj, indent): + spaces = ' ' * (indent + 1) + result = '' + for attr in sorted(obj.__dict__): + if attr.startswith('__') and attr.endswith('__'): + result += spaces + '{0}\n'.format(attr) + else: + result += spaces + '{0}={1}\n'.format(attr, getattr(obj, attr)) + return result + + def __listclass(self, aClass, indent): + dots = '.' * indent + if aClass in self.__visited: + return '\n{0}\n'.format( + dots, + aClass.__name__, + id(aClass)) + else: + self.__visited[aClass] = True + here = self.__attrnames(aClass, indent) + above = '' + for super in aClass.__bases__: + above += self.__listclass(super, indent + 4) + return '\n{0}'.format( + self.__class__.__name__, + id(self), + here, above) + + +def tester(listerclass, sept=False): + + class Super: + + def __init__(self): + self.data1 = 'spam' + + def ham(self): + pass + + class Sub(Super, listerclass): + + def __init__(self): + Super.__init__(self) + self.data2 = 'eggs' + self.data3 = 42 + + def spam(self): + pass + + instance = Sub() + print(instance) + if sept: + print('-' * 80) + + +if __name__ == '__main__': + + tester(ListTree) From 1bc65274ea185756feb11884cb82e1e83fa369de Mon Sep 17 00:00:00 2001 From: Abdul Moiz <33980210+progrmoiz@users.noreply.github.com> Date: Mon, 11 Dec 2017 21:45:22 +0500 Subject: [PATCH 03/22] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index a35a4b3..2e2b397 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ - [listinstance.py](listinstance.py) Mix-in class that provides a formatted print() or str() - [listinherited.py](listinherited.py) Mix-in class that provides a formatted print() or str() (Use dir() to collect both instance attr and names inherited from its classes) - [listinherited2.py](listinherited2.py) Same as listinherited.py, but more formatted +- [listtree.py](listtree.py) Trace the entire class and all its object's attrs at and above self --- From 9e9330924b2e8ee4e71fac68dac19bc43771a8f3 Mon Sep 17 00:00:00 2001 From: Abdul Moiz <33980210+progrmoiz@users.noreply.github.com> Date: Tue, 12 Dec 2017 02:05:59 +0500 Subject: [PATCH 04/22] Added Logo --- logo.png | Bin 0 -> 6158 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 logo.png diff --git a/logo.png b/logo.png new file mode 100644 index 0000000000000000000000000000000000000000..6e08e1d2cac87d09179c6c2fb1d3876bf80c74c9 GIT binary patch literal 6158 zcmXAt2QXaU`^L}iV)f`P%KGZPm#8aB^aRn%5+X~qh)!4%BnTV5Mekh%Ay`QyqC|@l zWYrKQ2%<*$ZT|ncGiUDHdCxP?ede5d-}8BP%lHNrIU6|ufJ#qS%M<_zl5np>N=JV$hxm7hopC`b1;Mco$P=VH&C0M7v!U_yZ(^JxwH|iOTOI zOO4yNELUzp>ObC(FbObMF|jg<)YiDoq_vIpL2*Oqd|#Xm&!!ol=B{TA8ipjF2JH=M z2KrA}qWp2@2lgwK6%`d2ULgqKiVP8iBoTFoC~mwOFol{z(qVy>cdSEz9ia#bEeL~`5S0*Z)Dk96&K72HWDtt5Kyd#5 z)kO#s4+7Ti_LdTg*kKEkohK~v52fgqif9=biLYSR1h1%Y-dvHm^nQq^Cr*MtjoU-$ zpFF3-^*1f$)rq6~1&4!rn#wkda``0EJhOwwcTKIm`6SG@Sb}PnrbHyt!YmHW1Tq|d z7yWs8Mn0{V{Lf+=(HTj1wt~=o(ioy1mm*?(zurebyrkX?{zk5!VYaI7&l zbO>J5y0>7Ir5K5(lIT<}DX`RucYLZd$<4Q51*HQV@%l`Zr9=)~%A~4ed?3d3(<`s0 ze+jj|XC%DjTAWiwprJ0P3Dn#kqBa12Ki`b|*Aa3!piRVH=z#=mWRs0(&RFsHT9m)zM1gxtUU4?w=!4 zxuREg^vJm3-LJw5iYT9^)yX&iwrOk8Bg52jWY0MLs#4qX33=!L%O>8|Zv z>h(10p2`bWm~z?sPh@6WWz!^t#Wce_5|%?c813FJ(*$s+CARcE9~I%Tw*2$$8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zR)zFd5_qk`kL_PRTF?SzZK1Y3O#-Ssr%4xOQz9eyed%%c%i;eaSZYgM!p;|oDV&qy zhBVAkwJ9r$LPlOLn@+VQ1FNYUiq}sCAd=TfsuCN8APtFB=#qdNP{)!)JD2?}W)OaG zpPI!GWn#h|9-MGJxXqU|c;bM34xU+^7@Hf9;tX;S5AU?I+%(Utu8BRcP%XB*$1&kA zG<-X)c~yHw>Q!u&*Fy4?bs#%Ys=>su%v&W^s(P=yF6dQQ`3>hP4VwfYBl!x} z=~U@N8;pR&0R1X&K@VxaF7xg&uTZ3q0U_TveB!ey1QP7?oB(UvT?Ki0{;}=FbPQ3q zkAYo_03U*HfsK%r-AX}-D&{^f0x}|}$<6wLAWzy#kdLuldZqf0kWc*!`+m%I>Hi4wG+tz9>h~5=G&iuR RJp|4V^t6q&>aIIR{SUjPIGO+e literal 0 HcmV?d00001 From 64cc22f4aa824c7c27171c978837360b0298d684 Mon Sep 17 00:00:00 2001 From: Abdul Moiz <33980210+progrmoiz@users.noreply.github.com> Date: Tue, 12 Dec 2017 02:07:03 +0500 Subject: [PATCH 05/22] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2e2b397..de8931d 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Python Snippets +# ![Python Snippets](./logo.png "Python Snippets") :snake: Python snippets of examples, to learn things and to make task easier. From f27d7778149873b637d8c9c48f086591a2a33c97 Mon Sep 17 00:00:00 2001 From: Moiz Date: Tue, 9 Jan 2018 01:58:05 +0500 Subject: [PATCH 06/22] mapattr --- README.md | 2 + mapattrs.py | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 mapattrs.py diff --git a/README.md b/README.md index de8931d..f9f4ca8 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,8 @@ - [listinherited.py](listinherited.py) Mix-in class that provides a formatted print() or str() (Use dir() to collect both instance attr and names inherited from its classes) - [listinherited2.py](listinherited2.py) Same as listinherited.py, but more formatted - [listtree.py](listtree.py) Trace the entire class and all its object's attrs at and above self +- [mapattrs.py](mapattrs.py) Map all attributes on or inherited by an +instance to the instance or class from which they are inherited. --- diff --git a/mapattrs.py b/mapattrs.py new file mode 100644 index 0000000..919323c --- /dev/null +++ b/mapattrs.py @@ -0,0 +1,106 @@ +""" +File: mapattrs.py (3.x + 2.x) + +Main tool: mapattrs() map all attributes on or inherited by an +instance t othe instance or class from which they are inherited. + +Assumes dir() give all attributes of an instance. To simulate +inheritance, uses eitehr the class's MRO tuple, which gives the +search order for new-style classes (and all in 3.x), or a recursive +traversal to infer the DFLR order of classic classes in 2.x + +Also here: inheritance() gives version-neutral class ordering; +assorted dictionary tools using 3.x/2.7 comprehensions. +""" + +import pprint + + +def trace(X, label='', end='\n'): + print(label + pprint.pformat(X) + end) # Print nicely + + +def filterdictvals(D, V): + """ + dict D with entries for valeus V removed. + filterdictvals(dict(a=1, b=2, c=1), 1) => {'b': 2} + """ + return {K: V2 for (K, V2) in D.items() if V2 != V} + + +def invertdict(D): + """ + dict D with values changed to keys (grouped by values). + Values must all be hashable to work as dict/set keys. + invertdict(dict(a=1, b=2, c=1)) => {1: ['a', 'c'], 2: 'b'} + """ + def keysof(V): + return sorted(K for K in D.keys() if D[K] == V) + return {V: keysof(V) for V in set(D.values())} + + +def dflr(cls): + """ + Classics depth-first left-to-right order of class tree at cls. + Cycles not possible: Python disallows on __bases__ changes. + """ + here = [cls] + for sup in cls.__bases: + here += dflr(sup) + return here + + +def inheritance(instance): + """ + Inheritance order sequnce: new-style (MRO) or classic (DFLR) + """ + if (hasattr(instance.__class__, '__mro__')): + return (instance,) + instance.__class__.__mro__ + else: + return [instance] + dflr(instance.__class__) + + +def mapattrs(instance, withobject=False, bysource=False): + """ + dict with keys giving all inherited attributes of instance, + with values giving the object that each is inherited from. + withobject: False=remove object built-in class attributes + bysource: True=group result by objects instead of attributes. + Supports classes with slot that preclude __dict__ in instance. + """ + attr2obj = {} + inherits = inheritance(instance) + for attr in dir(instance): + for obj in inherits: + if hasattr(obj, '__dict__') and attr in obj.__dict__: + attr2obj[attr] = obj + break + + if not withobject: + attr2obj = filterdictvals(attr2obj, object) + + return attr2obj if not bysource else invertdict(attr2obj) + + +if __name__ == '__main__': + print('Classics classes in 2.x, new-style in 3.x') + class A: attr1 = 1 + class B(A): attr2 = 2 + class C(A): attr1 = 3 + class D(B, C): pass + I = D() + print("Py=>%s" % I.attr1) + trace(inheritance(I), 'INH\n') + trace(mapattrs(I), 'ATTRS\n') + trace(mapattrs(I, bysource=True), 'OBJS\n') + + print('New-style classes in 2.x and 3.x') + class A(object): attr1 = 1 + class B(A): attr2 = 2 + class C(A): attr1 = 3 + class D(B, C): pass + I = D() + print("Py=>%s" % I.attr1) + trace(inheritance(I), 'INH\n') + trace(mapattrs(I), 'ATTRS\n') + trace(mapattrs(I, bysource=True), 'OBJS\n') From cb02363bea1550993417b0af6e07d3a3e7a00895 Mon Sep 17 00:00:00 2001 From: Moiz Date: Sun, 14 Jan 2018 18:52:21 +0500 Subject: [PATCH 07/22] computed property.py --- computedproperty.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 computedproperty.py diff --git a/computedproperty.py b/computedproperty.py new file mode 100644 index 0000000..84d58b2 --- /dev/null +++ b/computedproperty.py @@ -0,0 +1,38 @@ +ng computed property and setting + + +class operators(object): + def __getattr__(self, name): + if name == 'age': + return 40 + else: + raise AttributeError(name) + + def __setattr__(self, name, value): + print('set: %s %s' % (name, value)) + if name == 'age': + self.__dict__['_age'] = value + else: + self.__dict__[name] = value + + +# OR BETTER WAY + + +class properties(object): + def getage(self): + return 40 + + def setage(self, value): + self._age = value + + age = property(getage, setage, None, None) + + +if __name__ == '__main__': + x, y = operators(), properties() + for ins in (x, y): + print(ins.age) + ins.age = 20 + print(ins._age) + From ab9cbc86f32439dfd91078d2604f71f778c54d4e Mon Sep 17 00:00:00 2001 From: Moiz Date: Thu, 18 Jan 2018 10:57:12 +0500 Subject: [PATCH 08/22] state retention --- state_retention.py | 67 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 state_retention.py diff --git a/state_retention.py b/state_retention.py new file mode 100644 index 0000000..f5755b4 --- /dev/null +++ b/state_retention.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python3 +from __future__ import print_function + + +# nonlocal 3.x +def tester(start): + state = start + + def nested(label): + nonlocal state + print(label, state) + state += 1 + return nested + + +# nested global 2.x, 3.x +def tester1(start): + global gstate + gstate = start + + def nested(label): + global gstate + print(label, gstate) + gstate += 1 + return nested + + +# with mutables +def tester2(start): + state = [start] + + def nested(label): + print(label, state[0]) + state[0] += 1 + return nested + + +# function attr +def tester3(start): + + def nested(label): + print(label, nested.state) + nested.state += 1 + nested.state = 0 + return nested + + +# class +class tester4(object): + + def __init__(self, start): + self.state = start + + def __call__(self, label): + print(label, self.state) + self.state += 1 + + +if __name__ == '__main__': + for test in (tester, tester1, tester2, tester3, tester4): + f = test(0) + f('name: %s, state:' % test.__name__) + f('name: %s, state:' % test.__name__) + f('name: %s, state:' % test.__name__) + f('name: %s, state:' % test.__name__) + print() + print('done') From 5555a9323c84723786746fcfac80eb635a57bccb Mon Sep 17 00:00:00 2001 From: Abdul Moiz <33980210+progrmoiz@users.noreply.github.com> Date: Thu, 22 Aug 2019 09:50:41 +0500 Subject: [PATCH 09/22] Create FUNDING.yml --- .github/FUNDING.yml | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 .github/FUNDING.yml diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..1852419 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,4 @@ +# These are supported funding model platforms + +github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] + From d778b8f7610fd93602a6e79a38572ef00671986f Mon Sep 17 00:00:00 2001 From: Abdul Moiz <33980210+progrmoiz@users.noreply.github.com> Date: Thu, 22 Aug 2019 09:52:15 +0500 Subject: [PATCH 10/22] Update FUNDING.yml --- .github/FUNDING.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml index 1852419..12d9a20 100644 --- a/.github/FUNDING.yml +++ b/.github/FUNDING.yml @@ -1,4 +1,4 @@ # These are supported funding model platforms -github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] +github: progrmoiz From fb97cb6da5c5db7f7aa964ddbaae6c6bb8bc4e50 Mon Sep 17 00:00:00 2001 From: Saket <52213128+saket04@users.noreply.github.com> Date: Wed, 30 Sep 2020 20:36:57 +0530 Subject: [PATCH 11/22] update timer.py --- timer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/timer.py b/timer.py index d2be915..07f8c39 100644 --- a/timer.py +++ b/timer.py @@ -43,7 +43,7 @@ def bestof(reps, func, *pargs, **kargs): def bestoftotal(reps1, reps2, func, *pargs, **kargs): """ - Best of totals: + It will give best of total: (best of reps1 runs of (total of reps2 runs of func)) """ return bestof(reps1, total, reps2, func, *pargs, **kargs) From 26312db6bda0899a4ed29ac0ba67f2f40421909d Mon Sep 17 00:00:00 2001 From: Amitamit789 <72153736+Amitamit789@users.noreply.github.com> Date: Wed, 30 Sep 2020 22:13:57 +0530 Subject: [PATCH 12/22] Update CODE_OF_CONDUCT.md --- CODE_OF_CONDUCT.md | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md index 4c41aad..2272a81 100644 --- a/CODE_OF_CONDUCT.md +++ b/CODE_OF_CONDUCT.md @@ -8,11 +8,11 @@ In the interest of fostering an open and welcoming environment, we as contributo Examples of behavior that contributes to creating a positive environment include: -* Using welcoming and inclusive language -* Being respectful of differing viewpoints and experiences -* Gracefully accepting constructive criticism -* Focusing on what is best for the community -* Showing empathy towards other community members +* Using welcoming and inclusive language. +* Being respectful of differing viewpoints and experiences. +* Gracefully accepting constructive criticism. +* Focusing on what is best for the community. +* Showing empathy towards other community members. Examples of unacceptable behavior by participants include: @@ -28,19 +28,20 @@ Project maintainers are responsible for clarifying the standards of acceptable b Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. -## Scope +##Our Scope This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. -## Enforcement +##Our Enforcement Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at progrmoiz@gmail.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. -## Attribution +##Our Attribution -This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]. [homepage]: http://contributor-covenant.org [version]: http://contributor-covenant.org/version/1/4/ +Read carefully From fc6c3bc297f2f714db9cb8556a5aeefb0099d615 Mon Sep 17 00:00:00 2001 From: vinayduggal7 <60979457+vinayduggal7@users.noreply.github.com> Date: Thu, 1 Oct 2020 01:51:59 +0530 Subject: [PATCH 13/22] Update listtree.py --- listtree.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/listtree.py b/listtree.py index b1874b4..48507b1 100644 --- a/listtree.py +++ b/listtree.py @@ -51,22 +51,17 @@ def __str__(self): def tester(listerclass, sept=False): - class Super: - - def __init__(self): + def __init__(self): self.data1 = 'spam' - - def ham(self): + def ham(self): pass class Sub(Super, listerclass): - def __init__(self): Super.__init__(self) self.data2 = 'eggs' self.data3 = 42 - def spam(self): pass From 0019eb7c05246ffb9951679a0ac866693e7f42b3 Mon Sep 17 00:00:00 2001 From: vinayduggal7 <60979457+vinayduggal7@users.noreply.github.com> Date: Thu, 1 Oct 2020 01:53:28 +0530 Subject: [PATCH 14/22] Update mydir.py --- mydir.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mydir.py b/mydir.py index 26f70f8..4868927 100644 --- a/mydir.py +++ b/mydir.py @@ -1,6 +1,6 @@ #!python """ -mydir.py: a module that lists the namespaces of other module +mydir.py: a module that list the namespaces of other modules """ from __future__ import print_function From a48068a5f5291f3fc9378731977b3cd9a6295cb5 Mon Sep 17 00:00:00 2001 From: akshatbhatter1 <72153930+akshatbhatter1@users.noreply.github.com> Date: Thu, 1 Oct 2020 10:15:12 +0530 Subject: [PATCH 15/22] Update classtools.py --- classtools.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/classtools.py b/classtools.py index f3d8a79..5ee0436 100644 --- a/classtools.py +++ b/classtools.py @@ -32,5 +32,4 @@ class SubTest(TopTest): pass X, Y = TopTest(), SubTest() - print(X) - print(Y) + print(X,"\n",y) From 4f8f21ebbad2c8f997ec2fd6ff60a07ca15db72f Mon Sep 17 00:00:00 2001 From: kshitizkk <43603070+kshitizkk@users.noreply.github.com> Date: Thu, 1 Oct 2020 12:56:00 +0530 Subject: [PATCH 16/22] Update mydir.py --- mydir.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mydir.py b/mydir.py index 26f70f8..edfa4c0 100644 --- a/mydir.py +++ b/mydir.py @@ -25,7 +25,7 @@ def listing(module, verbose=True): if verbose: print(sepline) - print(module.__name__, 'has %d names' % count) + print(module.__name__, 'has following %d names' % count) print(sepline) if __name__ == '__main__': From 62340700011cf9e45fafe0edda75a4b0d201f3cb Mon Sep 17 00:00:00 2001 From: TanmayNagdeve <55934827+TanmayNagdeve@users.noreply.github.com> Date: Thu, 1 Oct 2020 16:03:35 +0530 Subject: [PATCH 17/22] Added 3 more snippets to the list title.py, list_comprehension.py, string_split.py --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index f9f4ca8..ebc0259 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,10 @@ - [listtree.py](listtree.py) Trace the entire class and all its object's attrs at and above self - [mapattrs.py](mapattrs.py) Map all attributes on or inherited by an instance to the instance or class from which they are inherited. +-[title.py](title.py) The following snippet can be used to convert a string to title case. This is done using the title() method of the string class. +-[list_comprehension.py](list_comprehension.py) List comprehension provides us with an elegant way of creating lists based on other lists. +-[string_split.py](string_split.py) We can split a string into a list of substrings using the .split() method in the string class. + --- From f26a2b2bda1d226c8c17f63c7be81af61b34617a Mon Sep 17 00:00:00 2001 From: tarunmishra007 <33347581+tarunmishra007@users.noreply.github.com> Date: Fri, 2 Oct 2020 00:03:57 +0530 Subject: [PATCH 18/22] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f9f4ca8..44ef5b4 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ --- -## Snippets +## Snippets TO LEARN - [string_import.py](string_import.py) example to show import module from string - [classtools.py](classtools.py) assorted class utilities and tools From 5a3edb521d0414fe7046ed68a733121371a695ef Mon Sep 17 00:00:00 2001 From: Gokul2406 <67820068+Gokul2406@users.noreply.github.com> Date: Fri, 2 Oct 2020 08:53:55 +0530 Subject: [PATCH 19/22] Improved Docs --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index f9f4ca8..e50e45b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,5 @@ # ![Python Snippets](./logo.png "Python Snippets") - -:snake: Python snippets of examples, to learn things and to make task easier. +These Snippets can make your life a lot easier than expected, use it wisely. --- From ee45bf08d7bb828d9858a2411cebb89f934d6971 Mon Sep 17 00:00:00 2001 From: Arpit045-boop <56675826+Arpit045-boop@users.noreply.github.com> Date: Sat, 3 Oct 2020 20:43:57 +0530 Subject: [PATCH 20/22] Update string_import.py --- string_import.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/string_import.py b/string_import.py index b70439d..fdd3d87 100644 --- a/string_import.py +++ b/string_import.py @@ -1,12 +1,12 @@ # you can use builtin __import__ function -modname = 'string' +modname = 'strings' string = __import__(modname) -print(string) +print(strings) # Python official prefferred way import importlib -modname = 'string' -string = importlib.import_module(modname) -print(string) +modname = 'strings' +strings = importlib.import_module(modname) +print(strings) From 9cb5b3a0850312620f244bee87b5efe86bd09624 Mon Sep 17 00:00:00 2001 From: Abdul Moiz <33980210+progrmoiz@users.noreply.github.com> Date: Fri, 9 Oct 2020 12:38:28 +0500 Subject: [PATCH 21/22] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 10a3ad7..fcaacc4 100644 --- a/README.md +++ b/README.md @@ -25,9 +25,9 @@ These Snippets can make your life a lot easier than expected, use it wisely. - [listtree.py](listtree.py) Trace the entire class and all its object's attrs at and above self - [mapattrs.py](mapattrs.py) Map all attributes on or inherited by an instance to the instance or class from which they are inherited. --[title.py](title.py) The following snippet can be used to convert a string to title case. This is done using the title() method of the string class. --[list_comprehension.py](list_comprehension.py) List comprehension provides us with an elegant way of creating lists based on other lists. --[string_split.py](string_split.py) We can split a string into a list of substrings using the .split() method in the string class. +- [title.py](title.py) The following snippet can be used to convert a string to title case. This is done using the title() method of the string class. +- [list_comprehension.py](list_comprehension.py) List comprehension provides us with an elegant way of creating lists based on other lists. +- [string_split.py](string_split.py) We can split a string into a list of substrings using the .split() method in the string class. --- From 0d782f966bb6d8197dab3fc541694db5c9123c90 Mon Sep 17 00:00:00 2001 From: Abdul Moiz <33980210+progrmoiz@users.noreply.github.com> Date: Fri, 9 Oct 2020 23:17:31 +0500 Subject: [PATCH 22/22] Update README.md --- README.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/README.md b/README.md index fcaacc4..aa9ea9c 100644 --- a/README.md +++ b/README.md @@ -25,9 +25,6 @@ These Snippets can make your life a lot easier than expected, use it wisely. - [listtree.py](listtree.py) Trace the entire class and all its object's attrs at and above self - [mapattrs.py](mapattrs.py) Map all attributes on or inherited by an instance to the instance or class from which they are inherited. -- [title.py](title.py) The following snippet can be used to convert a string to title case. This is done using the title() method of the string class. -- [list_comprehension.py](list_comprehension.py) List comprehension provides us with an elegant way of creating lists based on other lists. -- [string_split.py](string_split.py) We can split a string into a list of substrings using the .split() method in the string class. ---