Skip to content

Instantly share code, notes, and snippets.

View BexTuychiev's full-sized avatar
🏠
Working from home

bexgboost BexTuychiev

🏠
Working from home
View GitHub Profile
@BexTuychiev
BexTuychiev / basic_check.py
Created July 9, 2020 07:24
Type checking for the basic built-in objects in Python
variable = 'Python'
if type(variable) in [set, int, str, dict, list, float, tuple, bool]:
print('It is a built-in type')
else:
print('Maybe not a built-in?')
generator = (number for number in range(10))
if type(generator) == "<class 'generator'>":
print('Are you crazy?')
import types
generator = (number for number in range(10))
if isinstance(generator, types.GeneratorType):
print("Finally, a generator!")
# A function to reverse a string
def reverse_string(text: str, upper: bool = False) -> str:
reversed_text: str = text[::-1]
if upper:
return reversed_text.upper()
return reversed_text
def function_name(arg1: type1, opt_arg: type2 = default) -> return_type:
pass
import pandas as pd
"""Option One""""
# Load file as an ExcelFile object
excel = pd.ExcelFile('financial_sample.xlsx')
# Print out the sheet names
print(excel.sheet_names) # Prints out 'Sheet1' and 'Sheet2'
# Load the two sheets as individual DataFrames
# Importing matlab files
import scipy.io
import pandas as pd
filename = "ja_data2.mat"
matlab = scipy.io.loadmat(filename)
print(type(matlab)) # prints out 'class <dict>'
from sas7bdat import SAS7BDAT
import pandas as pd
with SAS7BDAT('skinproduct.sas7bdat') as file:
sas_df = file.to_data_frame()
print(sas_df)
# Importing a stata file
import pandas as pd
dta = pd.read_stata('cola.dta')
print(dta)
# Saving as a stata file
# Read any csv file or any DataFrame
df = pd.read_csv('sample.csv')
# Save as .dta file
import h5py
filename = 'ligo_data.hdf5'
data = h5py.File(filename, 'r')
print(data.keys())
# Values for each group can be accessed like this
print(data['meta'].value)