0% found this document useful (0 votes)
52 views9 pages

80838581

The document contains code snippets related to Python datetime and regular expression functions. It defines several functions: - func1() and func2() take datetime parameters like year, month, day etc. and return datetime objects after performing operations like subtraction and formatting. - Additional functions defined are for working with collections like namedtuple, deque, Counter, OrderedDict, defaultdict. - Regular expression functions match patterns in strings and extract matches. The document appears to contain examples and test functions for working with Python datetime and regular expressions. The functions perform common operations like formatting, matching, extracting from strings.

Uploaded by

ikhwancules46
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views9 pages

80838581

The document contains code snippets related to Python datetime and regular expression functions. It defines several functions: - func1() and func2() take datetime parameters like year, month, day etc. and return datetime objects after performing operations like subtraction and formatting. - Additional functions defined are for working with collections like namedtuple, deque, Counter, OrderedDict, defaultdict. - Regular expression functions match patterns in strings and extract matches. The document appears to contain examples and test functions for working with Python datetime and regular expressions. The functions perform common operations like formatting, matching, extracting from strings.

Uploaded by

ikhwancules46
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

# SYS FUNCTION

def func1(a):
s=sys.stdout.write(a)
return s

----------------------------
second part
python3 prog.py;py.test test.py

----------------------------
# second part
s=os.getcwd()
Newfolder="New Folder"
newpath=os.path.join(s,Newfolder)
#print(s)
os.mkdir(newpath)
os.chdir(newpath)
q=os.getcwd()
#print(q)
os.chdir(s)
#print(os.getcwd())
os.rename(Newfolder,"New Folder2")
Newfolder2="New Folder2"
newpath2=os.path.join(s,Newfolder2)
os.chdir(newpath2)
p=os.getcwd()
os.chdir(s)
os.rmdir(Newfolder2)
w=os.getcwd()
#print(w)

----------------------------
#TERCER PARTE

input_date = datetime.date(y,m,d)

input_date1=datetime.datetime(input_date.year,input_date.month,
input_date.day,ms,se,da,mi)

t=input_date1-datetime.timedelta(microseconds=1,seconds=2,days=3,minutes=2)
'''
#input_date1=input_date+datetime.timedelta(ms,se,da,mi)
t=input_date1-datetime.timedelta(1,2,3,2)
print(t)
day =t.strftime("%d")
year= t.strftime("%Y")
month =t.strftime("%B")
minute=t.strftime("%M")
second=t.strftime("%S")'''
return input_date,input_date1,t,0,0,0,0,0

------------------------------------------------------
#TERCERA PARTE V2
input_date = datetime.date(y,m,d)

input_date1=datetime.datetime(input_date.year,input_date.month,
input_date.day,ms,se,da,mi)

t=input_date1-datetime.timedelta(microseconds=1,seconds=2,days=3,minutes=2)
day =t.strftime("%d")
'''
#input_date1=input_date+datetime.timedelta(ms,se,da,mi)
t=input_date1-datetime.timedelta(1,2,3,2)
print(t)
day =t.strftime("%d")
year= t.strftime("%Y")
month =t.strftime("%B")
minute=t.strftime("%M")
second=t.strftime("%S")'''
return input_date,input_date1,t,day,0,0,0,0

2008-05-08
2008-05-08 01:02:03.000005
2008-05-05 01:00:01.000004
5
2008
5
0
1
2008-05-08 01:02:03.000005
Thu May 08 01:02: 03 2008
2008-05-08 01:02:03
Expected Output

Download
2008-05-08
2008-05-08 01:02:03.000005
2008-05-05 01:00:01.000004
5
2008
5
0
1
2008-05-08 01:02:03.000005
Thu May 08 01 02: 03 2008
2008-05-08 01:02:03

----------------------------------------------------
def func1(y,m,d,ms,se,da,mi):

input_date = datetime.date(y,m,d)

input_date1=datetime.datetime(input_date.year,input_date.month,
input_date.day,ms,se,da,mi)

t=input_date1-datetime.timedelta(microseconds=1,seconds=2,days=3,minutes=2)
day =t.strftime("%d")
year=t.strftime("%Y")
month=t.strftime("%m")
minute=t.strftime("%M")
second=t.strftime("%S")

return
input_date,input_date1,t,int(day)//1,year,int(month)//1,int(minute)//1,int(secon
d)//1

# return input_date,input_date1,t,day,year,month,minute,second
def func2(y,m,d,ms,se,da,mu):
#print('muv{}'.format(mu))
date_=datetime.date(2020,1,3)
date_=datetime.datetime(date_.year,date_.month, date_.day,10,24,34)
date_=date_.strftime("%a %b %d %H:%M:%S %Y")
#print(date_)
s=date_
x=datetime.date(y,m,d)
x=datetime.datetime(x.year,x.month, x.day,ms,se,da,mu)

q=x.strftime("%a %b %d %H %M: %S %Y")


z=datetime.datetime.strftime(x, '%Y-%m-%d %H:%M:%S')
return x,q,z
-------------------------------------------------------
def func1(y,m,d,ms,se,da,mi):

input_date = datetime.date(y,m,d)

input_date1=datetime.datetime(input_date.year,input_date.month,
input_date.day,ms,se,da,mi)

t=input_date1-datetime.timedelta(microseconds=1,seconds=2,days=3,minutes=2)
day =t.strftime("%d")
year=t.strftime("%Y")
month=t.strftime("%m")
minute=t.strftime("%M")
second=t.strftime("%S")

return
input_date,input_date1,t,int(day)//1,year,int(month)//1,int(minute)//1,int(secon
d)//1

# return input_date,input_date1,t,day,year,month,minute,second
def func2(y,m,d,ms,se,da,mu):
#print('muv{}'.format(mu))
date_=datetime.date(2020,1,3)
date_=datetime.datetime(date_.year,date_.month, date_.day,10,24,34)
date_=date_.strftime("%a %b %d %H:%M:%S %Y")
#print(date_)
s=date_
x=datetime.date(y,m,d)
m=5
x=datetime.datetime(x.year,x.month, x.day,ms,se,da,m)

q=x.strftime("%a %b %d %H %M: %S %Y")


z=datetime.datetime.strftime(x, '%Y-%m-%d %H:%M:%S')
return x,q,z

-------------------------------------------

ULTIMO

import os
import shutil
# Source path

source_="projects/challenge/New Dir/newww.txt"
s=os.getcwd()
Newfolder="New Dir/newww.txt"
newpath=os.path.join(s,Newfolder)
source =newpath

# Destination path
#destination ="projects/challenge"
destination =s
#print(os.listdir(newpath))

# Copy the content of


# source to destination
dest =shutil.copy(source, destination)

# Print path of newly


# created file
print("Destination path:", dest)

with open('.hidden.txt','w') as outfile:


outfile.write(dest)

------------------------
deque
import os
from collections import *

import collections
# Enter your code here.
#namedtuple
def func1(x,y):
player =collections.namedtuple('player', 'name runs')

s= player(name=x, runs=y)
return s
#deque
def func2(s):
list_=list(list(s))
d=deque(list_)
return d
#Counter
def func3(x):
e=collections.Counter(x)
return e
#Ordereddict
def func4(m,n,o,p,q):
val_=list(range(1,6))
key_=[m,n,o,p,q]
dir_=zip(val_,key_)
d=dict(dir_)
#d=OrderedDict(sorted(d.items(), key=lambda t: t[1]))
d=OrderedDict(sorted(d.items(), key=lambda t: t[0]))

return d
#defaultdict
def func5(a,b):
s=defaultdict(list)
s[0]=(a)
s[1]=(b)

return s
'''For testing the code, no input is to be provided'''

---------------------------------------------
Built in 2 - sample case -2
def func1(x,y):
player =collections.namedtuple('player', 'name runs')

s= player(name=x, runs=y)
return s
#deque
def func2(s):
list_=list(list(s))
list_=list(list_)
d=deque([list_])
return d
#Counter
def func3(x):
e=collections.Counter(x)
return e
#Ordereddict
def func4(m,n,o,p,q):
val_=list(range(1,6))
key_=[m,n,o,p,q]
dir_=zip(val_,key_)
d=dict(dir_)
#d=OrderedDict(sorted(d.items(), key=lambda t: t[1]))
d=OrderedDict(sorted(d.items(), key=lambda t: t[0]))

return d
#defaultdict
def func5(a,b):
s=defaultdict(list)
s[0]=(a)
s[1]=(b)

return s
'''For testing the code, no input is to be provided'''

------------------------------------------------
Regular Expressions - Hands On 2
def function(a):
split_=a.split()

r1=bool(re.match('e',split_[0]))
r2=bool(re.match('e',split_[1]))
if(r1==True and r2==True):
m=True
else:
m=False
return m

regex nuevo:

def function(a):

regex = '^e\w+\se\w+'

result=bool(re.match(regex,a))

return result
------------------------------------------------
Regular Expressions - Hands On 1
def function(a):
regex = r'^[aeiouAEIOU][aeiouAEIOU]*'
if(re.search(regex, a)):
match=True
else:
match=False

return match
------------------------------------------------
Regular Expressions - Hands On 3
def function(a):
regex = '[A-Za-z]'

result=bool(re.match(regex,a))

return result

------------------------------------------------
Regular Expressions - Hands On 4
def main(x):
#print(x)
pattern="[A-Z]\S*"
pattern2="[0-9]\S*"

names=re.findall(pattern,x)
values=re.findall(pattern2,x)

dicts=dict(zip(names,values))

return dicts
'''For testing the code, no input is to be provided'''

-------------------------------------------
r=str(sample_text)
#print(r)
hosts = r.split(",")
#print(hosts)
#match = re.search(r"[0-9]\S*", hosts[2])
#match = re.search(r"[a-z]\S*", hosts[3])
match = re.search(r"[a-z]\S*|[0-9]\S*", hosts[2])

print(match.group())

--------------------------------------------
import os
import re
import re
# Enter your code here
sample_text=['199.72.81.55 - - [01/Jul/1995:00:00:01 -0400] "GET
/history/apollo/ HTTP/1.0" 200 6245',
'unicomp6.unicomp.net - - [01/Jul/1995:00:00:06 -0400] "GET /shuttle/countdown/
HTTP/1.0" 200 3985',
'199.120.110.21 - - [01/Jul/1995:00:00:09 -0400] "GET /shuttle/missions/sts-
73/mission-sts-73.html HTTP/1.0" 200 4085',
'burger.letters.com - - [01/Jul/1995:00:00:11 -0400] "GET
/shuttle/countdown/liftoff.html HTTP/1.0" 304 0',
'199.120.110.21 - - [01/Jul/1995:00:00:11 -0400] "GET /shuttle/missions/sts-
73/sts-73-patch-small.gif HTTP/1.0" 200 4179',
'burger.letters.com - - [01/Jul/1995:00:00:12 -0400] "GET /images/NASA-
logosmall.gif HTTP/1.0" 304 0',
'burger.letters.com - - [01/Jul/1995:00:00:12 -0400] "GET
/shuttle/countdown/video/livevideo.gif HTTP/1.0" 200 0',
'205.212.115.106 - - [01/Jul/1995:00:00:12 -0400] "GET
/shuttle/countdown/countdown.html HTTP/1.0" 200 3985',
'd104.aa.net - - [01/Jul/1995:00:00:13 -0400] "GET /shuttle/countdown/
HTTP/1.0" 200 3985',
'129.94.144.152 - - [01/Jul/1995:00:00:13 -0400] "GET / HTTP/1.0" 200 7074',
'unicomp6.unicomp.net - - [01/Jul/1995:00:00:14 -0400] "GET
/shuttle/countdown/count.gif HTTP/1.0" 200 40310',
'unicomp6.unicomp.net - - [01/Jul/1995:00:00:14 -0400] "GET /images/NASA-
logosmall.gif HTTP/1.0" 200 786',
'unicomp6.unicomp.net - - [01/Jul/1995:00:00:14 -0400] "GET /images/KSC-
logosmall.gif HTTP/1.0" 200 1204',
'd104.aa.net - - [01/Jul/1995:00:00:15 -0400] "GET /shuttle/countdown/count.gif
HTTP/1.0" 200 40310',
'd104.aa.net - - [01/Jul/1995:00:00:15 -0400] "GET /images/NASA-logosmall.gif
HTTP/1.0" 200 786']
def func1():
r=str(sample_text)
#print(r)
hosts = r.split(",")
#print(hosts)
#match = re.search(r"[0-9]\S*", hosts[2])
#match = re.search(r"[a-z]\S*", hosts[3])
list_=[]
for i in hosts:
match = re.search(r"[a-z]\S*|[0-9]\S*", i)
list_.append(match.group())

print(list_)

def func2():
r=str(sample_text)
#print(r)
hosts = r.split(",")
match = re.search(r"\[(.*?)\]\s", hosts[0])
timestamps=match.group()

print(timestamps)
def func3():
method_uri_protocol=7

print(method_uri_protocol)

def func4():
status=7

print(status)

def func5():
content_size=7

print(content_size)
'''For testing the code, no input is to be provided'''

------------------------------------------------
Regular Expressions - Hands On 5

# Enter your code here


sample_text=['199.72.81.55 - - [01/Jul/1995:00:00:01 -0400] "GET
/history/apollo/ HTTP/1.0" 200 6245',
'unicomp6.unicomp.net - - [01/Jul/1995:00:00:06 -0400] "GET /shuttle/countdown/
HTTP/1.0" 200 3985',
'199.120.110.21 - - [01/Jul/1995:00:00:09 -0400] "GET /shuttle/missions/sts-
73/mission-sts-73.html HTTP/1.0" 200 4085',
'burger.letters.com - - [01/Jul/1995:00:00:11 -0400] "GET
/shuttle/countdown/liftoff.html HTTP/1.0" 304 0',
'199.120.110.21 - - [01/Jul/1995:00:00:11 -0400] "GET /shuttle/missions/sts-
73/sts-73-patch-small.gif HTTP/1.0" 200 4179',
'burger.letters.com - - [01/Jul/1995:00:00:12 -0400] "GET /images/NASA-
logosmall.gif HTTP/1.0" 304 0',
'burger.letters.com - - [01/Jul/1995:00:00:12 -0400] "GET
/shuttle/countdown/video/livevideo.gif HTTP/1.0" 200 0',
'205.212.115.106 - - [01/Jul/1995:00:00:12 -0400] "GET
/shuttle/countdown/countdown.html HTTP/1.0" 200 3985',
'd104.aa.net - - [01/Jul/1995:00:00:13 -0400] "GET /shuttle/countdown/
HTTP/1.0" 200 3985',
'129.94.144.152 - - [01/Jul/1995:00:00:13 -0400] "GET / HTTP/1.0" 200 7074',
'unicomp6.unicomp.net - - [01/Jul/1995:00:00:14 -0400] "GET
/shuttle/countdown/count.gif HTTP/1.0" 200 40310',
'unicomp6.unicomp.net - - [01/Jul/1995:00:00:14 -0400] "GET /images/NASA-
logosmall.gif HTTP/1.0" 200 786',
'unicomp6.unicomp.net - - [01/Jul/1995:00:00:14 -0400] "GET /images/KSC-
logosmall.gif HTTP/1.0" 200 1204',
'd104.aa.net - - [01/Jul/1995:00:00:15 -0400] "GET /shuttle/countdown/count.gif
HTTP/1.0" 200 40310',
'd104.aa.net - - [01/Jul/1995:00:00:15 -0400] "GET /images/NASA-logosmall.gif
HTTP/1.0" 200 786']

def func1():
r=str(sample_text)

hosts = r.split(",")

list_=[]
for i in hosts:
match = re.search(r"[a-z]\S*|[0-9]\S*", i)
list_.append(match.group())

host=list_
print(host)

def func2():
r=str(sample_text)
#print(r)
hosts = r.split(",")
#match = re.search(r"\[(.*?)\]\s", hosts[0])
#match = re.search(r"\[[0-9/]+\w{3}", hosts[0])
#match = re.search(r"\[[0-9A-Za-z/:]+\s[0-9-]+\]", hosts[0])
list_=[]
for i in hosts:
match = re.search(r"01\/Jul\/1995\:[0-9:]+\s[0-9-]+", i)
list_.append(match.group())
timestamps=list_
print(timestamps)

def func3():
r=str(sample_text)
#print(r)
hosts = r.split(",")
list_=[]
for i in hosts:
match = re.search(r"(GET)\s(\B\/{1}[A-Za-z]*\/*[A-Za-z.-]*\/*[a-
z.]*\/*[A-Za-z.-]*[73/.-]*\/*[A-Za-z.-]*[0-9a-zA-z.-]*)\s(HTTP/1.0)", i)
list_.append(match.groups())

#match2 = re.search(r"\B\/{1}[A-Za-z]*\/*[A-Za-z.-]*\/*[a-z.]*\/*[A-Za-
z.-]*[73/.-]*\/*[A-Za-z.-]*[0-9a-zA-z.-]*", i)
#match3 = re.search(r"HTTP/1.0", hosts[0])
#method_uri_protocol=(match.group())
#method_uri_protocol2=(match2.group())
#method_uri_protocol3=(match3.group())
#superstr=str(method_uri_protocol)+', '+str(method_uri_protocol2) +',
'+str(method_uri_protocol3)

#list_.append('\''+str(method_uri_protocol)+'\''+', '+
'\''+str(method_uri_protocol2)+'\''+', '+'\''+str(method_uri_protocol3)+'\'')

#newstr=str(list_)

#newstr.replace("[","")
method_uri_protocol_pattern=list_
print(method_uri_protocol_pattern)

def func4():
r=str(sample_text)

hosts = r.split(",")

list_=[]
for i in hosts:
match = re.search(r"[23]0[40]", i)
list_.append(match.group())
status_pattern=list_

print(status_pattern)

def func5():
r=str(sample_text)

hosts = r.split(",")

list_=[]
for i in hosts:

match = re.search(r"(\d+)(?!.*\d)", i)
list_.append(match.group())
content_size=list_

print(content_size)
'''For testing the code, no input is to be provided'''

-------------------------------------------

You might also like