मेरे टेलीग्राम-चैनल @pythonetc से टिप्स और ट्रिक्स, नवंबर 2019
यह मेरे टेलीग्राम-चैनल @pythonetc से पायथन और प्रोग्रामिंग के बारे में सुझावों और ट्रिक्स का एक नया चयन है।
Ations
पिछला प्रकाशन ।
PATH
एक पर्यावरण चर है जो पथों को संग्रहीत करता है जहां निष्पादन योग्य वस्तुओं की तलाश की जाती है। जब आप अपने शेल को
ls
चलाने के लिए कहते हैं, तो शेल PATH में प्रस्तुत किए गए सभी रास्तों पर
ls
निष्पादन योग्य फ़ाइल की तलाश करता है।
$ echo $PATH /usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/v.pushtaev/.local/bin:/home/v.pushtaev/bin $ which ls /usr/bin/ls
उदाहरण में ऊपर के रास्तों को अलग किया गया है
:
पथ में। कोई बचना संभव नहीं है: एक पथ जिसमें शामिल है
:
PATH
अंदर उपयोग नहीं किया जा सकता है।
हालाँकि, यह सभी ऑपरेटिंग सिस्टम के लिए सही नहीं है। अजगर में आप
os.pathsep
साथ स्थानीय सिस्टम के लिए सही विभाजक प्राप्त कर सकते हैं:
Python 3.5.0 [...] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> os.pathsep ';'
os.pathsep
को
os.pathsep
साथ नहीं
os.path.sep
जो फ़ाइल पथों के लिए विभाजक है:
>>> os.path.sep '/'
नियमित अभिव्यक्ति को अधिक पठनीय बनाने के लिए आप
re.VERBOSE
ध्वज का उपयोग कर सकते हैं। यह आपको अतिरिक्त स्थानों का उपयोग करने की अनुमति देता है, जहाँ भी आप चाहें और साथ ही
#
प्रतीक के साथ टिप्पणी जोड़ सकते हैं:
import re URL_RE = re.compile(r''' ^ (https?):// (www[.])? ( (?: [^.]+[.] )+ ( [^/]+ ) # TLD ) (/.*) $ ''', re.VERBOSE) m = URL_RE.match('https://www.pythonetc.com/about/') schema, www, domain, tld, path = m.groups() has_www: bool = bool(www) print(f'schema={schema}, has_www={has_www}') print(f'domain={domain}, tld={tld}') print(f'path={path}')
re.X
लिए एक उपनाम है।
complex
है पायथन में निर्मित जटिल संख्याओं के लिए प्रकार:
>>> complex(1, 2).real 1.0 >>> abs(complex(3, 4)) 5.0 >>> complex(1, 2) == complex(1, -2).conjugate() True >>> str(complex(2, -3)) '(2-3j)'
हालांकि इसे सीधे इस्तेमाल करने की आवश्यकता नहीं है क्योंकि पायथन में जटिल संख्याओं के लिए शाब्दिक हैं:
>>> (3 + 4j).imag 4.0 >>> not (3 + 4j) False >>> (-3 - 4j) + (2 - 2j) (-1-6j)
a : b : c
नोटेशन का उपयोग केवल कोष्ठक के भीतर
slice(a, b, c)
को परिभाषित करने के लिए किया जा सकता है:
>>> [1, 2, 3, 4, 5][0:4:2] [1, 3] >>> [1, 2, 3, 4, 5][slice(0, 4, 2)] [1, 3]
यदि आप किसी फ़ंक्शन के तर्क के रूप में स्लाइस ऑब्जेक्ट को पास करना चाहते हैं, तो आपको इसे स्पष्ट रूप से परिभाषित करना होगा:
def multislice(slc, *iterables): return [i[slc] for i in iterables] print(multislice( slice(2, 6, 2), [1, 2, 3, 4, 5, 6, 7], [2, 4, 2, 4, 2, 4, 2], ))
यहां बताया गया है कि आप इस तरह के फ़ंक्शन को एक ऑब्जेक्ट में बदल सकते हैं जो
[a : b : c]
समर्थन करता है:
from functools import partial class SliceArgDecorator: def __init__(self, f): self._f = f def __getitem__(self, slc): return partial(self._f, slc) slice_arg = SliceArgDecorator @slice_arg def multislice(slc, *iterables): return [i[slc] for i in iterables] print(multislice[2:6:2]( [1, 2, 3, 4, 5, 6, 7], [2, 4, 2, 4, 2, 4, 2], ))
__getattribute__
एक शक्तिशाली उपकरण है जो आपको उपयुक्त होने पर प्रतिनिधि पैटर्न का आसानी से उपयोग करने की अनुमति देता है। यहां बताया गया है कि आप एक गैर-तुलनीय वस्तु के साथ तुलना करने की क्षमता कैसे जोड़ सकते हैं:
class CustomEq: def __init__(self, orig, *, key): self._orig = orig self._key = key def __lt__(self, other): return self._key(self) < self._key(other) def __getattribute__(self, name): if name in {'_key', '_orig', '__lt__'}: return super().__getattribute__(name) return getattr(self._orig, name) class User: def __init__(self, user_id): self._user_id = user_id def get_user_id(self): return self._user_id def comparable(obj, *, key): return CustomEq(obj, key=key) user1 = comparable(User(1), key=lambda u: u.get_user_id()) user2 = comparable(User(2), key=lambda u: u.get_user_id()) print(user2 > user1)