def path_examples(): print_header('Path examples') filepath = "~/pyexample.txt" # Expand shortcut to an abs path to a home dir filepath = path.expanduser(filepath) print(f'Path to pyexample: {filepath}') # Check if file exists print(f'Path {filepath} exists: {path.exists(filepath)}') print(f'Path {filepath} is a file: {path.isfile(filepath)}') print(f'Path {filepath} is a directory: {path.isdir(filepath)}') parent_dir = '..' print(f'Parent dir abs path: {path.abspath(parent_dir)}') # Print relative path current_dir = path.realpath(path.normpath(path.curdir)) print( f'Relative path to {filepath} from {current_dir}: {path.relpath(filepath)}' ) # Symlinks behaviour symlink_path = path.expanduser('~/.bashrc') print(f'Abspath to a symlink {symlink_path}: {path.abspath(symlink_path)}') print( f'Realpath to a symlink {symlink_path}: {path.abspath(symlink_path)}')
def file_example(file_path=TEST_FILE_PATH): print_header('Writing to pyexample.txt in a home directory') # Open file for writing; create if not exists f = open(file_path, 'w+') for i in range(10): f.write(f'Line number: {i}\n') f.close() # Open in an append mode f = open(file_path, 'a') for i in range(10): f.write(f'Line number: {i}\n') f.close() # Reading all content f = open(file_path, 'r') if f.mode == 'r': contents = f.read() print(contents) f.close() # Reading line by line f = open(file_path, 'r') if f.mode == 'r': fl = f.readlines() for line in fl: print(line.strip()) f.close()
def csv_module_example(): print_header('CSV example') from pprint import pprint src_file = f'{os.getcwd()}{os.sep}pysharpen{os.sep}dataengineering{os.sep}ingestion{os.sep}taxi.csv.bz2' for idx, row in enumerate(iter_records(src_file)): if idx > 10: break pprint(row)
def zipfile_examples(): print_header('zipfile examples') root_dir, fn = path.split(TEST_FILE_PATH) zip_path = root_dir + os.sep + 'pyexample2.zip' with ZipFile(zip_path, 'w') as zf: zf.start_dir zf.write(TEST_FILE_PATH) zf.write(root_dir + os.sep + fn + '.bak')
def file_attributes_example(): print_header(header='File attributes examples') p = path.realpath(path.expanduser('~/pyexample.txt')) # Get modification time in seconds from Epoch, convert it to string t = time.ctime(path.getmtime(p)) print(f'Modification time (ctime): {t}') t = datetime.datetime.fromtimestamp(path.getmtime(p)) print(f'Modification time (datetime.fromtimestamp): {t}')
def html_parsing_examples(): print_header('HTML parsing examples') cwd = os.getcwd() filepath = f'{cwd}{os.sep}pysharpen{os.sep}internet{os.sep}samplehtml.html' with open(filepath, 'r') as f: if f.mode == 'r': contents = f.read() my_parser = MyHTMLParser() my_parser.feed(contents)
def json_examples(): print_header('JSON examples') url = 'https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson' resp = req.urlopen(url) if resp.getcode() == 200: data = resp.read() print_results(data) else: print(f'Something went wrong. Status code: {resp.getcode()}')
def timedelta_examples(): print_header('Timedelta examples') from datetime import timedelta, datetime td = timedelta(days=365, hours=10, minutes=2) print(f'Timedelta 365 days {td}') now = datetime.now() year_from_now = now + timedelta(days=365) print(f'Year from now {year_from_now}') print(f'In 3 weeks and 2 days it will be {now + timedelta(weeks=3, days=2)}')
def pandas_example(): print_header('Pandas example') from pprint import pprint src_file = f'{os.getcwd()}{os.sep}pysharpen{os.sep}dataengineering{os.sep}ingestion{os.sep}taxi.csv.bz2' with bz2.open(src_file, 'rt') as f: df = pd.read_csv(f, parse_dates=['tpep_pickup_datetime', 'tpep_dropoff_datetime']) for idx, row in df.iterrows(): if idx > 10: break pprint(row)
def xml_parsing_examples(): print_header('XML parsing examples') filepath = f'{os.getcwd()}{os.sep}pysharpen{os.sep}internet{os.sep}samplexml.xml' doc = xml.dom.minidom.parse(filepath) print(f'First node name: {doc.nodeName}') print(f'First child tag name: {doc.firstChild.tagName}') skills = doc.getElementsByTagName('skill') for skill in skills: print(f'Has skill: {skill.getAttribute("name")}') new_skill = doc.createElement('skill') new_skill.setAttribute('name', 'Microsoft Azure') doc.firstChild.appendChild(new_skill)
def datetime_examples(): print_header('Datetime examples') # Get current date and time datetime = dt.datetime now = datetime.now() print(f'Now is: {now}') time_now = datetime.time(now) print(f'Time now is: {time_now}') # Format using strftime function # %Y - year, %m - month (1-12), %d - day of month(1-31) # %b - short month abbreviation (Jan-Dec), %B - full month name # %c - locale's date and time, %x - locale's date, %X - locale's time # %a - abbreviated day name, %A - full day name # %I/%H - 12/24 hour format, %M - minute, %S - second, %p - locale's AM/PM format print("Today using locale's date and time format", now.strftime('%c')) print("Time using 12 hour and AM/PM format", now.strftime('%I %p'))
def date_examples(): print_header('Date examples') # Print today's date date = dt.date today = date.today() print(f'Today is: {today}') # Print date components: year, month, day of month print(f'Year: {today.year}, month: {today.month}, day: {today.day}') # Print weekday where 0==Monday, 6==Sunday print(f'Weekday is: {today.weekday()}') # Format using strftime function # %Y - year, %m - month (1-12), %d - day of month(1-31) # %b - short month abbreviation (Jan-Dec), %B - full month name # %c - locale's date and time, %x - locale's date, %X - locale's time # %a - abbreviated day name, %A - full day name print('Today using dd-mm-yyyy format', today.strftime('%d-%m-%Y')) print("Today using locale's date format", today.strftime('%x'))
def shell_examples(): print_header('Shell examples') src = TEST_FILE_PATH dst = TEST_FILE_PATH + '.bak' dst1 = TEST_FILE_PATH + '.bak1' # Copy file shutil.copy(src, dst) #Copy all file attributes shutil.copystat(src, dst) # Rename file root_dir, name = path.split(src) # new_name = root_dir + '/pyexample_new.txt' # if path.exists(new_name): # os.remove(new_name) # os.rename(src, new_name) # Make zip archive make_archive(base_name='pytestarch', format='zip', root_dir=root_dir, base_dir=path.realpath(path.expanduser('~/Documents')))
def http_examples(): print_header('HTTP examples') web_url = req.urlopen('https://google.com') print(f'Response code: {web_url.getcode()}') print(f'Response data: {web_url.read()}')