Skip to content

Instantly share code, notes, and snippets.

@ZhouYang1993
ZhouYang1993 / regex_example.py
Created April 19, 2020 22:14
regex in Python
import re
re.match(r'\d{5}','12345')
# <_sre.SRE_Match object; span=(0, 5), match='12345'>
re.match(r'\d{5}','1234')
# None
@ZhouYang1993
ZhouYang1993 / regex_example2.py
Created April 19, 2020 22:24
Regex in Python
>>> 'a b c'.split(' ')
# ['a', 'b', '', '', 'c'] Can't split by consecutive spaces.
>>> re.split(r'\s+', 'a b c')
# ['a', 'b', 'c']
>>> re.split(r'[\s\,]+', 'a,b, c d')
# ['a', 'b', 'c', 'd']
>>> re.split(r'[\s\,\;]+', 'a,b;; c d')
@ZhouYang1993
ZhouYang1993 / regex_example3.py
Created April 19, 2020 22:35
Regex in Python
time='18:05'
matched = re.match(r'^(0[0-9]|1[0-9]|2[0-3])\:(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9])$', time)
matched.groups()
# ('18', '05') show all groups(substrings)
matched.group()
# '18:05' group will always be the original string
matched.group(0)
# '18:05' group(0) will always be the original string
matched.group(1)
# '18'
@ZhouYang1993
ZhouYang1993 / regex_example3.py
Created April 19, 2020 22:39
Regex in Python
time='18:05'
matched = re.match(r'^(0[0-9]|1[0-9]|2[0-3])\:(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9])$', time)
matched.groups()
# ('18', '05')
matched.group()
# '18:05'
matched.group(0)
# '18:05' group(0) returns the original string
matched.group(1)
# '18'
@ZhouYang1993
ZhouYang1993 / regex_example4.py
Created April 19, 2020 22:55
Regex in Python
import re
re_three_numbers = re.compile(r'^\d{3}$')
re_three_numbers.match('123')
# <_sre.SRE_Match object; span=(0, 3), match='123'>
re_three_numbers.match('12345')
# None
@ZhouYang1993
ZhouYang1993 / example1.py
Created April 22, 2020 10:02
Differences between Class Attribute and Instance Attribute of Python
class MyClass(object):
class_attr = 1
def __init__(self, value):
self.instance_attr = value
@ZhouYang1993
ZhouYang1993 / example2.py
Created April 22, 2020 10:26
Differences between Class Attribute and Instance Attribute of Python
class MyClass(object):
class_attr = 0
def __init__(self, instance_attr):
self.instance_attr = instance_attr
MyClass.class_attr
# 0
MyClass.instace_attr
# Traceback (most recent call last):
# File "/usr/lib/python3.6/code.py", line 91, in runcode
@ZhouYang1993
ZhouYang1993 / example3.py
Created April 22, 2020 10:48
Differences between Class Attribute and Instance Attribute of Python
class MyClass(object):
class_attr = 0
def __init__(self, instance_attr):
self.instance_attr = instance_attr
print(MyClass.__dict__)
# {'__module__': '__main__', 'class_attr': 0, '__init__': <function MyClass.__init__ at 0x7f00cb7fcea0>, '__dict__': <attribute '__dict__' of 'MyClass' objects>, '__weakref__': <attribute '__weakref__' of 'MyClass' objects>, '__doc__': None}
my_instance = MyClass(1)
print(my_instance.__dict__)
# {'instance_attr': 1}
@ZhouYang1993
ZhouYang1993 / example4.py
Last active April 22, 2020 11:20
Differences between Class Attribute and Instance Attribute of Python
class HandsomeGuy(object):
name = 'Yang'
def __init__(self, value):
self.name = value
guy = HandsomeGuy('Jack')
HandsomeGuy.name
# 'Yang'
guy.name
# 'Jack'
@ZhouYang1993
ZhouYang1993 / example5.py
Created April 22, 2020 12:23
Differences between Class Attribute and Instance Attribute of Python
class MyClass(object):
data = 0
def __init__(self, value):
self.instance_data = value
MyClass.data
# 0
my_instance = MyClass(3)
my_instance.data