Skip to content

Instantly share code, notes, and snippets.

View kshirsagarsiddharth's full-sized avatar
🎯
Focusing

kshirsagarsiddharth

🎯
Focusing
View GitHub Profile
from collections import namedtuple
Bevrage = namedtuple('Bevrage',['name','color','type'])
# defining a class in which named tuple acts as a constructor or #__init__() method
class BeverageContainer(Bevrage):
def container_type(self):
if self.type == 'hot':
return "Use a Mug"
else:
return "A mug is not required"
@kshirsagarsiddharth
kshirsagarsiddharth / computing.py
Last active July 18, 2020 17:55
computing using tuples
# consider a shopping list which contains various objects with quantity and cost and this items are stored in a list as tuples with their description and we want to find the total cost with discount
from collections import namedtuple
grocery = namedtuple('grocery',['item_name','item_weight','item_cost',
'item_quantity','item_dicsount'])
shopping_list_tuples = [('apples',2.3,128,7,13.6),('chicken',1,228,2,110),
('milk',0.6,18,2,0),('lemons',0.3,45,20,50)]
@kshirsagarsiddharth
kshirsagarsiddharth / opening_file.py
Created July 19, 2020 10:50
opening and reading a website or any file in python
from urllib.request import urlopen
def read_data(name: str):
if name.startswith(('http:','https:','ftp:')):
return urlopen(name).read()
else:
with open(name) is f:
return f.read()
from collections import namedtuple
animal = namedtuple('animal',['species_name','legs_no','color'])
dog = animal('canine',4,'white')
dog.legs_no = 78
@kshirsagarsiddharth
kshirsagarsiddharth / replace.py
Created July 19, 2020 17:23
using replace method on namedtuple
from collections import namedtuple
animal = namedtuple('animal',['species_name','legs_no','color'])
dog = animal('canine',4,'white')
dog = dog._replace(legs_no = 6)
print(dog.legs_no)
# Output: 6
@kshirsagarsiddharth
kshirsagarsiddharth / replace.py
Created July 20, 2020 07:31
replacing texts in python
text = "HELLO WORLD, hello world, Hello World".
re.sub("hello",'good morning',text,flags=re.IGNORECASE)
# Output: 'good morning WORLD, good morning world, good morning World'
@kshirsagarsiddharth
kshirsagarsiddharth / replace2.py
Created July 20, 2020 07:44
replacing with keeping cases constant
def matchcase(word):
def replace(m):
text = m.group()
if text.isupper():
return word.upper()
elif text.islower():
return word.lower()
elif text[0].isupper():
return word.capitalize()
else:
@kshirsagarsiddharth
kshirsagarsiddharth / decomposition.py
Created July 21, 2020 05:07
unicode decomposition
s1 = 'Jalape\u00f1o' # composed form
s2 = 'Jalapen\u0303o' # decomposed form
print(s1,s2)
# Output: Jalapeño Jalapeño
print(s1 == s2)
# Output: False
from unicodedata import normalize
v1 = normalize('NFC',s1)
v2 = normalize('NFC',s2)
import unicodedata
s3 = "tête-à-tête"
s4 = unicodedata.normalize('NFD',s3)
print(len(s3) == len(s4))
# Output: False
[val for val in s4 if unicodedata.combining(val)]
# Output: ['̂', '̀', '̂']
@kshirsagarsiddharth
kshirsagarsiddharth / accent_removal.py
Last active July 21, 2020 07:12
removing accents of unicode strings
text_to_translate = "īīŏŏūāāīīĕĕĕŏōūў ūūĭĭŏŏŭŭēīūūўĕŏūūȳēēīū ēēīīŏŏōōōō"
import unicodedata
import sys
dictonary_of_accents = dict.fromkeys(val for val in range(sys.maxunicode) if
unicodedata.combining(chr(val)))
# here we are using dict.fromkeys(keys,value) method of the dict class which returns a dictonary.
#with specified keys and values in this case the values is not specified which means that the value of the keys is none
normalized_text = unicodedata.normalize('NFD',text_to_translate)