{"id":1929,"date":"2019-06-17T14:27:37","date_gmt":"2019-06-17T14:27:37","guid":{"rendered":"http:\/\/wiki.thomasandsofia.com\/?p=1929"},"modified":"2019-06-18T02:23:57","modified_gmt":"2019-06-18T02:23:57","slug":"section-15-advanced-python-modules","status":"publish","type":"post","link":"https:\/\/wiki.thomasandsofia.com\/?p=1929","title":{"rendered":"Section 15: Advanced Python Modules"},"content":{"rendered":"<p>&lt; Section 14 | <a href=\"http:\/\/wiki.thomasandsofia.com\/?p=1938\">Section 16 &gt;<\/a><\/p>\n<h1>Collections Module<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/3512826?start=0#questions\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/3512826?start=0#questions<\/a><\/p>\n<p>The collections module is a built in module that implements specialized container datatypes providing alternatives to python&#8217;s general purpose build-in containers.\u00a0 We&#8217;ve already discussed several of the basics: dict, list, set and tuple<\/p>\n<h2>Counter<\/h2>\n<p>Counter counts the number of time an item appears in a list.<\/p>\n<p>Counter is a dict subclass which helps count hashable objects.\u00a0 Inside of it elements are stored as dictionary keys and the counts of the objects are stored as the value<\/p>\n<pre>from collections import Counter\r\nfrom collections import Counter\r\nmylist = [1,1,1,1,2,2,3,3,3,4,4,2,2,5,1,5,2,5,4,6,6,7]\r\nCounter(mylist)<\/pre>\n<p>Counter({1: 5, 2: 5, 3: 3, 4: 3, 5: 3, 6: 2, 7: 1})<\/p>\n<pre>s = 'Mississippi'\r\nCounter(s)<\/pre>\n<p>Counter({&#8216;M&#8217;: 1, &#8216;i&#8217;: 4, &#8216;s&#8217;: 4, &#8216;p&#8217;: 2})<\/p>\n<h3>Count the word repititions in a sentence<\/h3>\n<pre>s = \"How many Up times does times show times uP in show this UP sentence\"\r\nCounter(s.lower().split()<\/pre>\n<p>Counter({&#8216;how&#8217;: 1,<br \/>\n&#8216;many&#8217;: 1,<br \/>\n&#8216;up&#8217;: 3,<br \/>\n&#8216;times&#8217;: 3,<br \/>\n&#8216;does&#8217;: 1,<br \/>\n&#8216;show&#8217;: 2,<br \/>\n&#8216;in&#8217;: 1,<br \/>\n&#8216;this&#8217;: 1,<br \/>\n&#8216;sentence&#8217;: 1})<\/p>\n<h3>Common methods<\/h3>\n<pre>s = \"How many Up times does times show times uP in show this UP sentence\"\r\nwords = s.lower().split()\r\nc = Counter(words)<\/pre>\n<h4>.most_common(n)<\/h4>\n<pre>c.most_common(2)<\/pre>\n<p>[(&#8216;up&#8217;, 3), (&#8216;times&#8217;, 3)]<\/p>\n<p><b>To get the Least Common elements:<\/b><br \/>\n.most_common(:-n-1:-1)<br \/>\nHint: Use a negative step<\/p>\n<pre>c.most_common()[:-2-1:-1]<\/pre>\n<p>[(&#8216;sentence&#8217;, 1), (&#8216;this&#8217;, 1)]<\/p>\n<h4>List()<\/h4>\n<p>Shows unique elements<\/p>\n<pre>list(c)<\/pre>\n<p>[&#8216;how&#8217;, &#8216;many&#8217;, &#8216;up&#8217;, &#8216;times&#8217;, &#8216;does&#8217;, &#8216;show&#8217;, &#8216;in&#8217;, &#8216;this&#8217;, &#8216;sentence&#8217;]<\/p>\n<h4>sum(.values())<\/h4>\n<p>Total of all counts<\/p>\n<pre>sum(c.values())<\/pre>\n<p>14<\/p>\n<h1>defaultdict<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/3512824#questions\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/3512824#questions<\/a><\/p>\n<p>defaultdict is a dictionary like oibject which provides all methods provided by dictionary, but takes first argument (default_factory) as default data type for the dictionary.\u00a0 using defaultdict is faster than doing the same using dict.set_default method.<\/p>\n<p>A defaultdict will never raise a KeyError.\u00a0 Any key that does not exist gets the value returned by the default factory.<\/p>\n<pre>from collections import defaultdict\r\nd = {}\r\nd['one']<\/pre>\n<p>KeyError: &#8216;one&#8217;<\/p>\n<pre>d = defaultdict(object)\r\nd['one']\r\nfor item in d:\r\n    print(item)<\/pre>\n<p>one<\/p>\n<h3>Assign default values to 0 using lambda<\/h3>\n<pre>d = defaultdict(lambda: 0)\r\nd['two]<\/pre>\n<p>0<\/p>\n<pre>d['three']=3\r\nd['three']<\/pre>\n<p>3<\/p>\n<h1>Ordered Dictionaries OrderedDict<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/3779906#questions\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/3779906#questions<\/a><\/p>\n<p>Standard dict object do not retain a specific order<\/p>\n<pre>d = {}\r\nd['a']=1\r\nd['b']=2\r\nd['c']=3\r\nd['d']=4\r\nd['e']=5\r\nfor k,v in d.items():\r\n    print( k,v)<\/pre>\n<p>a 1<br \/>\nb 2<br \/>\ne 5<br \/>\nd 4<br \/>\nc 3<\/p>\n<pre>from collections import OrderedDict\r\nd = orderedDict()\r\nd['a']=1\r\nd['b']=2\r\nd['c']=3\r\nd['d']=4\r\nd['e']=5\r\nfor k,v in d.items():\r\n    print( k,v)<\/pre>\n<p>a 1<br \/>\nb 2<br \/>\nc 3<br \/>\nd 4<br \/>\ne 5<\/p>\n<h3>With OrderedDict, order is important<\/h3>\n<pre>d1={'a':1, 'b':2}\r\nd2={'b':2, 'a':1}\r\nd1 == d2<\/pre>\n<p>True<br \/>\n<b>this would fail with OrderedDict, because the order of the keys would not match.<\/b><\/p>\n<h1>namedtuple<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/3512830#questions\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/3512830#questions<\/a><\/p>\n<p>These are similar to creating classes with both named and indexed values.<\/p>\n<p>Named values are sent as a string with spaces between each key value.<\/p>\n<pre>from collections import namedtuple\r\nDog = namedtuple('Dog','age breed name')\r\nsam = Dog(age=2, breed='Lab', name='Sammy')\r\nprint(sam.breed)\r\nprint(sam[0])<\/pre>\n<p>Lab<br \/>\n2<\/p>\n<h1>Datetime<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/3547908#questions\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/3547908#questions<\/a><\/p>\n<h2>datetime.time<\/h2>\n<pre>import datetime\r\n#datetime.time( hours, minutes, seconds, microseconds)\r\nt = datetime.time(5, 25, 1)\r\nprint(t.hour)\r\nprint(t.minute)\r\nprint(t.second)\r\nprint(t.microsecond)\r\nprint(t.resolution<\/pre>\n<p>5<br \/>\n25<br \/>\n1<br \/>\n0<br \/>\n0:00:00.000001<\/p>\n<h2>datetime.date<\/h2>\n<pre>today = datetime.date.today()\r\nprint(today)<\/pre>\n<p>2019-06-17<\/p>\n<pre>today.timetuple()<\/pre>\n<p>time.struct_time(tm_year=2019, tm_mon=6, tm_mday=17, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=168, tm_isdst=-1)<\/p>\n<pre>d1 = datetime.date(2019, 6, 17)\r\nd2 = d1.replace(year = 2018)\r\nprint(d2)<\/pre>\n<p>2018-06-17<\/p>\n<h3>Math on Dates<\/h3>\n<pre>lastyear = today.replace(year = 2018)\r\nprint(today - lastyear)<\/pre>\n<p>365 days, 0:00:00<\/p>\n<h1>Python Debugger pdb<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/3547912#questions\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/3547912#questions<\/a><\/p>\n<p>Tool good for tracing through your code and being able to see where errors occur.<\/p>\n<pre>import pdb\r\nx = [1,3,5]\r\ny = 4\r\nz = 7\r\nresult = y +z\r\nprint(result)\r\nresult2 = x + y\r\nprint(result2)<\/pre>\n<p>11<br \/>\nTypeError: can only concatenate list (not &#8220;int&#8221;) to list<\/p>\n<p>Modify your code to stop before the error point and perform some checking<br \/>\n<b>To stop, press &#8216;Q&#8217;<\/b><\/p>\n<pre>import pdb\r\nx = [1,3,5]\r\ny = 4\r\nz = 7\r\nresult = y +z\r\nprint(result)\r\npdb.set_trace()\r\nresult2 = x + y\r\nprint(result2)<\/pre>\n<p>11<\/p>\n<pre>(Pdb) x<\/pre>\n<p>[1, 3, 5]<\/p>\n<h1>Timing your Code<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/3547914#questions\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/3547914#questions<\/a><\/p>\n<p>This is useful for checking small sections of code for optimizing.\u00a0 Running these small snipits multiple times will provide more accurate results than a single iteration.<\/p>\n<p><b>The command is passed to the function as a string.<\/b><\/p>\n<pre>import timeit\r\n# How long does it take to create the string: '0-1-2-3...99'\r\ntimeit.timeit('\"-\".join(str(n) for n in range(100))', number = 10000)<\/pre>\n<p>0.6442793710000387<\/p>\n<pre># as a list comprehension\r\ntimeit.timeit('\"-\".join([str(n) for n in range(100)])', number=10000)<\/pre>\n<p>0.5642282049999494<\/p>\n<pre># as a map function\r\ntimeit.timeit('\"-\".join(map(str, range(100)))', number=10000)<\/pre>\n<p>0.3922121389999802<\/p>\n<h2>To use Jupiter Notebooks built in magic fucntion<\/h2>\n<pre>%timeit \"-\".join(str(n) for n in range(100))<\/pre>\n<p>61.9 \u00b5s \u00b1 1.04 \u00b5s per loop (mean \u00b1 std. dev. of 7 runs, 10000 loops each)<\/p>\n<pre>%timeit \"-\".join(map(str, range(100)))<\/pre>\n<p>39.4 \u00b5s \u00b1 1.28 \u00b5s per loop (mean \u00b1 std. dev. of 7 runs, 10000 loops each)<\/p>\n<h1>Regular Expressions &#8211; re<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/3547904#questions\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/3547904#questions<\/a><\/p>\n<p>These are a common interview question!<\/p>\n<p>Regular expressions are text matching patters used for<\/p>\n<ul>\n<li>finding repetition<\/li>\n<li>matching patterns<\/li>\n<\/ul>\n<p>Basic search<\/p>\n<pre>import re\r\nre.search('hello','hello world')<\/pre>\n<p>&lt;re.Match object; span=(0, 5), match=&#8217;hello&#8217;&gt;<br \/>\nNotice this is a &#8216;Match&#8217; object<\/p>\n<pre>import re\r\npatterns = ['term1', 'term2'] \r\ntext = 'This is a string with term2, but not the other term'\r\nfor pattern in patterns:\r\n    print(f\"Searching for {pattern} in:\\n{text}\")\r\n    if re.search(pattern, text):\r\n        print(\"\\nMatch found!\\n\")\r\n    else:\r\n        print(\"\\nNo match found.\\n\")<\/pre>\n<p>Searching for term1 in:<br \/>\nThis is a string with term2, but not the other term<\/p>\n<p>No match found.<\/p>\n<p>Searching for term2 in:<br \/>\nThis is a string with term2, but not the other term<\/p>\n<p>Match found!<\/p>\n<h2>Object type<\/h2>\n<p>No match = NoneType<br \/>\nmatch = re.Match<\/p>\n<pre>match = re.search(patterns[0],text)\r\ntype(match)<\/pre>\n<p>NoneType<\/p>\n<pre>match = re.search(patterns[1],text)\r\ntype(match)<\/pre>\n<p>re.Match<\/p>\n<h2>Methods on Match<\/h2>\n<pre>match.start()<\/pre>\n<p>22<\/p>\n<pre>match.end()<\/pre>\n<p>27<\/p>\n<h2>Splitting with Regular Expressions<\/h2>\n<pre>splitterm = '@'\r\nphrase = \"Is your email address hello@gmail.com?\"\r\nre.split(splitterm, phrase)<\/pre>\n<p>[&#8216;Is your email address hello&#8217;, &#8216;gmail.com?&#8217;]<\/p>\n<h2>findall<\/h2>\n<pre>re.findall('match', 'Here is one match and here is another match')<\/pre>\n<p>[&#8216;match&#8217;, &#8216;match&#8217;]<\/p>\n<h2>Using Meta Characters<\/h2>\n<pre>def multi_re_find(patterns,phrase):\r\n    '''\r\n    Takes in a list of regex patterns\r\n    Prints a list of all matches\r\n    '''\r\n    for pattern in patterns:\r\n        print('Searching the phrase using the re check: %r' %(pattern))\r\n        print(re.findall(pattern,phrase))\r\n        print('\\n')<\/pre>\n<h3>Repetition Syntax<\/h3>\n<p>There are five ways to express repetition in a pattern:<\/p>\n<ul>\n<li>A pattern followed by the meta-character * is repeated zero or more times.<\/li>\n<li>Replace the * with + and the pattern must appear at least once.<\/li>\n<li>Using ? means the pattern appears zero or one time.<\/li>\n<li>For a specific number of occurrences, use {m} after the pattern, where m is replaced with the number of times the pattern should repeat.<\/li>\n<li>Use {m,n} where m** is the minimum number of repetitions and **n is the maximum. Leaving out n** {m,} means the value appears at least **m times, with no maximum.<\/li>\n<\/ul>\n<pre>test_phrase = 'sdsd..sssddd...sdddsddd...dsds...dsssss...sdddd'\r\n\r\ntest_patterns = [ 'sd*',     # s followed by zero or more d's\r\n                'sd+',          # s followed by one or more d's\r\n                'sd?',          # s followed by zero or one d's\r\n                'sd{3}',        # s followed by three d's\r\n                'sd{2,3}',      # s followed by two to three d's\r\n                ]\r\n\r\nmulti_re_find(test_patterns,test_phrase)<\/pre>\n<p>Searching the phrase using the re check: &#8216;sd*&#8217;<br \/>\n[&#8216;sd&#8217;, &#8216;sd&#8217;, &#8216;s&#8217;, &#8216;s&#8217;, &#8216;sddd&#8217;, &#8216;sddd&#8217;, &#8216;sddd&#8217;, &#8216;sd&#8217;, &#8216;s&#8217;, &#8216;s&#8217;, &#8216;s&#8217;, &#8216;s&#8217;, &#8216;s&#8217;, &#8216;s&#8217;, &#8216;sdddd&#8217;]<\/p>\n<p>Searching the phrase using the re check: &#8216;sd+&#8217;<br \/>\n[&#8216;sd&#8217;, &#8216;sd&#8217;, &#8216;sddd&#8217;, &#8216;sddd&#8217;, &#8216;sddd&#8217;, &#8216;sd&#8217;, &#8216;sdddd&#8217;]<\/p>\n<p>Searching the phrase using the re check: &#8216;sd?&#8217;<br \/>\n[&#8216;sd&#8217;, &#8216;sd&#8217;, &#8216;s&#8217;, &#8216;s&#8217;, &#8216;sd&#8217;, &#8216;sd&#8217;, &#8216;sd&#8217;, &#8216;sd&#8217;, &#8216;s&#8217;, &#8216;s&#8217;, &#8216;s&#8217;, &#8216;s&#8217;, &#8216;s&#8217;, &#8216;s&#8217;, &#8216;sd&#8217;]<\/p>\n<p>Searching the phrase using the re check: &#8216;sd{3}&#8217;<br \/>\n[&#8216;sddd&#8217;, &#8216;sddd&#8217;, &#8216;sddd&#8217;, &#8216;sddd&#8217;]<\/p>\n<p>Searching the phrase using the re check: &#8216;sd{2,3}&#8217;<br \/>\n[&#8216;sddd&#8217;, &#8216;sddd&#8217;, &#8216;sddd&#8217;, &#8216;sddd&#8217;]<\/p>\n<h3>Character Sets<\/h3>\n<p>(Think of a list without commas) [ab] = &#8216;a&#8217; or &#8216;b&#8217;<br \/>\nCharacter sets are used when you wish to match any one of a group of characters at a point in the input. Brackets are used to construct character set inputs. For example: the input <code>[ab]<\/code> searches for occurrences of either <strong>a** or **b<\/strong>. Let&#8217;s see some examples:<\/p>\n<pre>test_phrase = 'sdsd..sssddd...sdddsddd...dsds...dsssss...sdddd'\r\n\r\ntest_patterns = ['[sd]',    # either s or d\r\n                's[sd]+']   # s followed by one or more s or d\r\n\r\nmulti_re_find(test_patterns,test_phrase)<\/pre>\n<p>Searching the phrase using the re check: &#8216;[sd]&#8217;<br \/>\n[&#8216;s&#8217;, &#8216;d&#8217;, &#8216;s&#8217;, &#8216;d&#8217;, &#8216;s&#8217;, &#8216;s&#8217;, &#8216;s&#8217;, &#8216;d&#8217;, &#8216;d&#8217;, &#8216;d&#8217;, &#8216;s&#8217;, &#8216;d&#8217;, &#8216;d&#8217;, &#8216;d&#8217;, &#8216;s&#8217;, &#8216;d&#8217;, &#8216;d&#8217;, &#8216;d&#8217;, &#8216;d&#8217;, &#8216;s&#8217;, &#8216;d&#8217;, &#8216;s&#8217;, &#8216;d&#8217;, &#8216;s&#8217;, &#8216;s&#8217;, &#8216;s&#8217;, &#8216;s&#8217;, &#8216;s&#8217;, &#8216;s&#8217;, &#8216;d&#8217;, &#8216;d&#8217;, &#8216;d&#8217;, &#8216;d&#8217;]<\/p>\n<p>Searching the phrase using the re check: &#8216;s[sd]+&#8217;<br \/>\n[&#8216;sdsd&#8217;, &#8216;sssddd&#8217;, &#8216;sdddsddd&#8217;, &#8216;sds&#8217;, &#8216;sssss&#8217;, &#8216;sdddd&#8217;]<\/p>\n<h3>Exclusion<\/h3>\n<p>We can use ^ to exclude terms by incorporating it into the bracket syntax notation. For example: [^&#8230;] will match any single character not in the brackets. Let&#8217;s see some examples:<\/p>\n<pre>test_phrase = 'This is a string! But it has punctuation. How can we remove it?'\r\nre.findall('[^!.? ]+',test_phrase)<\/pre>\n<p>[&#8216;This&#8217;, &#8216;is&#8217;, &#8216;a&#8217;, &#8216;string&#8217;, &#8216;But&#8217;, &#8216;it&#8217;, &#8216;has&#8217;, &#8216;punctuation&#8217;, &#8216;How&#8217;, &#8216;can&#8217;, &#8216;we&#8217;, &#8216;remove&#8217;, &#8216;it&#8217;]<\/p>\n<h3>Character Ranges<\/h3>\n<p>As character sets grow larger, typing every character that should (or should not) match could become very tedious. A more compact format using character ranges lets you define a character set to include all of the contiguous characters between a start and stop point. The format used is [start-end].<\/p>\n<p>Common use cases are to search for a specific range of letters in the alphabet. For instance, [a-f] would return matches with any occurrence of letters between a and f.<\/p>\n<p>Let&#8217;s walk through some examples:<\/p>\n<pre>test_phrase = 'This is an example sentence. Lets see if we can find some letters.'\r\n\r\ntest_patterns=['[a-z]+',      # sequences of lower case letters\r\n               '[A-Z]+',      # sequences of upper case letters\r\n               '[a-zA-Z]+',   # sequences of lower or upper case letters\r\n               '[A-Z][a-z]+'] # one upper case letter followed by lower case letters\r\n                \r\nmulti_re_find(test_patterns,test_phrase)<\/pre>\n<p>Searching the phrase using the re check: &#8216;[a-z]+&#8217;<br \/>\n[&#8216;his&#8217;, &#8216;is&#8217;, &#8216;an&#8217;, &#8216;example&#8217;, &#8216;sentence&#8217;, &#8216;ets&#8217;, &#8216;see&#8217;, &#8216;if&#8217;, &#8216;we&#8217;, &#8216;can&#8217;, &#8216;find&#8217;, &#8216;some&#8217;, &#8216;letters&#8217;]<\/p>\n<p>Searching the phrase using the re check: &#8216;[A-Z]+&#8217;<br \/>\n[&#8216;T&#8217;, &#8216;L&#8217;]<\/p>\n<p>Searching the phrase using the re check: &#8216;[a-zA-Z]+&#8217;<br \/>\n[&#8216;This&#8217;, &#8216;is&#8217;, &#8216;an&#8217;, &#8216;example&#8217;, &#8216;sentence&#8217;, &#8216;Lets&#8217;, &#8216;see&#8217;, &#8216;if&#8217;, &#8216;we&#8217;, &#8216;can&#8217;, &#8216;find&#8217;, &#8216;some&#8217;, &#8216;letters&#8217;]<\/p>\n<p>Searching the phrase using the re check: &#8216;[A-Z][a-z]+&#8217;<br \/>\n[&#8216;This&#8217;, &#8216;Lets&#8217;]<\/p>\n<h3>Escape Codes<\/h3>\n<p>You can use special escape codes to find specific types of patterns in your data, such as digits, non-digits, whitespace, and more. For example:<\/p>\n<table class=\"docutils\" border=\"1\">\n<colgroup>\n<col width=\"14%\" \/>\n<col width=\"86%\" \/> <\/colgroup>\n<thead valign=\"bottom\">\n<tr class=\"row-odd\">\n<th class=\"head\">Code<\/th>\n<th class=\"head\">Meaning<\/th>\n<\/tr>\n<\/thead>\n<tbody valign=\"top\">\n<tr class=\"row-even\">\n<td><tt class=\"docutils literal\"><span class=\"pre\">\\d<\/span><\/tt><\/td>\n<td>a digit<\/td>\n<\/tr>\n<tr class=\"row-odd\">\n<td><tt class=\"docutils literal\"><span class=\"pre\">\\D<\/span><\/tt><\/td>\n<td>a non-digit<\/td>\n<\/tr>\n<tr class=\"row-even\">\n<td><tt class=\"docutils literal\"><span class=\"pre\">\\s<\/span><\/tt><\/td>\n<td>whitespace (tab, space, newline, etc.)<\/td>\n<\/tr>\n<tr class=\"row-odd\">\n<td><tt class=\"docutils literal\"><span class=\"pre\">\\S<\/span><\/tt><\/td>\n<td>non-whitespace<\/td>\n<\/tr>\n<tr class=\"row-even\">\n<td><tt class=\"docutils literal\"><span class=\"pre\">\\w<\/span><\/tt><\/td>\n<td>alphanumeric<\/td>\n<\/tr>\n<tr class=\"row-odd\">\n<td><tt class=\"docutils literal\"><span class=\"pre\">\\W<\/span><\/tt><\/td>\n<td>non-alphanumeric<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Escapes are indicated by prefixing the character with a backslash <code>r<\/code>, eliminates this problem and maintains readability.<\/p>\n<p>Personally, I think this use of <code>r<\/code> to escape a backslash is probably one of the things that block someone who is not familiar with regex in Python from being able to read regex code at first. Hopefully after seeing these examples this syntax will become clear.<\/p>\n<pre>test_phrase = 'This is a string with some numbers 1233 and a symbol #hashtag'\r\n\r\ntest_patterns=[ r'\\d+', # sequence of digits\r\n                r'\\D+', # sequence of non-digits\r\n                r'\\s+', # sequence of whitespace\r\n                r'\\S+', # sequence of non-whitespace\r\n                r'\\w+', # alphanumeric characters\r\n                r'\\W+', # non-alphanumeric\r\n                ]\r\n\r\nmulti_re_find(test_patterns,test_phrase)<\/pre>\n<p>Searching the phrase using the re check: &#8216;\\\\d+&#8217;<br \/>\n[&#8216;1233&#8217;]<\/p>\n<p>Searching the phrase using the re check: &#8216;\\\\D+&#8217;<br \/>\n[&#8216;This is a string with some numbers &#8216;, &#8216; and a symbol #hashtag&#8217;]<\/p>\n<p>Searching the phrase using the re check: &#8216;\\\\s+&#8217;<br \/>\n[&#8216; &#8216;, &#8216; &#8216;, &#8216; &#8216;, &#8216; &#8216;, &#8216; &#8216;, &#8216; &#8216;, &#8216; &#8216;, &#8216; &#8216;, &#8216; &#8216;, &#8216; &#8216;, &#8216; &#8216;]<\/p>\n<p>Searching the phrase using the re check: &#8216;\\\\S+&#8217;<br \/>\n[&#8216;This&#8217;, &#8216;is&#8217;, &#8216;a&#8217;, &#8216;string&#8217;, &#8216;with&#8217;, &#8216;some&#8217;, &#8216;numbers&#8217;, &#8216;1233&#8217;, &#8216;and&#8217;, &#8216;a&#8217;, &#8216;symbol&#8217;, &#8216;#hashtag&#8217;]<\/p>\n<p>Searching the phrase using the re check: &#8216;\\\\w+&#8217;<br \/>\n[&#8216;This&#8217;, &#8216;is&#8217;, &#8216;a&#8217;, &#8216;string&#8217;, &#8216;with&#8217;, &#8216;some&#8217;, &#8216;numbers&#8217;, &#8216;1233&#8217;, &#8216;and&#8217;, &#8216;a&#8217;, &#8216;symbol&#8217;, &#8216;hashtag&#8217;]<\/p>\n<p>Searching the phrase using the re check: &#8216;\\\\W+&#8217;<br \/>\n[&#8216; &#8216;, &#8216; &#8216;, &#8216; &#8216;, &#8216; &#8216;, &#8216; &#8216;, &#8216; &#8216;, &#8216; &#8216;, &#8216; &#8216;, &#8216; &#8216;, &#8216; &#8216;, &#8216; #&#8217;]<\/p>\n<h1>StringIO<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/3547906#questions\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/3547906#questions<\/a><\/p>\n<p>The StringIP module implements an in-memory file like object.\u00a0 This object can then be used as input or output to most functions that would expect a standard file object.<\/p>\n<pre>import StringIO\r\nmessage = \"This is just a normal string.\"\r\nf = StringIO.StringIO(message)\r\nf.read()\r\nf.write()\r\nf.seek(0)<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&lt; Section 14 | Section 16 &gt; Collections Module https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/3512826?start=0#questions The collections module is a built in module that implements specialized container datatypes providing alternatives to python&#8217;s general purpose build-in containers.\u00a0 We&#8217;ve already discussed several of the basics: dict, list, set and tuple Counter Counter counts the number of time an item appears in a ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/wiki.thomasandsofia.com\/?p=1929\" title=\"read more...\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[],"class_list":["post-1929","post","type-post","status-publish","format-standard","hentry","category-python-bootcamp-0-to-hero"],"_links":{"self":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/1929","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1929"}],"version-history":[{"count":9,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/1929\/revisions"}],"predecessor-version":[{"id":1940,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/1929\/revisions\/1940"}],"wp:attachment":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1929"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1929"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1929"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}