Last active
February 25, 2021 08:23
-
-
Save isennkubilay/4507b8a61213ae1125b0cd4f98173ca7 to your computer and use it in GitHub Desktop.
useful commands
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import namedtuple | |
def coroutine(func): | |
def start(*args, **kwargs): | |
cr = func(*args, **kwargs) | |
next(cr) | |
return cr | |
return start | |
# @coroutine | |
# def my_coroutine(a): | |
# """Yield expression will yield every value sent to the coroutine | |
# and assign that value to the variable on the left | |
# Coroutines are consumers of data. Coroutines are just another concurrency model | |
# Generators produce data for iteration | |
# """ | |
# print(f"Started with {a}") | |
# b = yield | |
# print(f"But continues with {b}") | |
@coroutine | |
def averager_with_result(): | |
Result = namedtuple("Result",["Count", "Average"]) | |
total = 0 | |
sum = 0 | |
average = None | |
while True: | |
value = yield average | |
if value is None: | |
break | |
total += 1 | |
sum += value | |
average = sum / total | |
return Result(total, average) | |
# averager = averager_with_result() | |
# averager.send(2) | |
# averager.send(4) | |
# averager.send(1) | |
# averager.send(5) | |
# averager.send(None) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Augmenting functions without modifications | |
""" | |
#Without Decorator | |
def adder(a,b): | |
return a + b | |
## print(adder(2,3)) --> 5 | |
####### | |
####### | |
#With Decorator | |
def decorat(func): | |
def times_10(x,y): | |
return func(x*10, y*10) | |
return times_10 | |
@decorat | |
def adder(a,b): | |
return a + b | |
## print(adder(2,3)) --> 50 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fibonacci(input_value): | |
if input_value == 1: | |
return 1 | |
elif input_value ==2: | |
return 1 | |
else: | |
return fibonacci_memo(input_value - 1) + fibonacci_memo(input_value - 2) | |
fibonacci_cache = {} | |
def fibonacci_memo(input_value): | |
if input_value in fibonacci_cache: | |
return fibonacci_cache[input_value] | |
if input_value == 1: | |
value = 1 | |
elif input_value == 2: | |
value = 1 | |
elif input_value > 2: | |
value = fibonacci_memo(input_value - 1) + fibonacci_memo(input_value - 2) | |
fibonacci_cache[input_value] = value | |
return value |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pip install PyPDF2 | |
from PyPDF2 import PdfFileWriter, PdfFileReader | |
def add_encryption(input_pdf, output_pdf, password): | |
pdf_writer = PdfFileWriter() | |
pdf_reader = PdfFileReader(input_pdf) | |
for page in range(pdf_reader.getNumPages()): | |
pdf_writer.addPage(pdf_reader.getPage(page)) | |
pdf_writer.encrypt(user_pwd=password, owner_pwd=None, | |
use_128bit=True) | |
with open(output_pdf, 'wb') as fh: | |
pdf_writer.write(fh) | |
if __name__ == '__main__': | |
add_encryption(input_pdf='merged.pdf', | |
output_pdf='merged-encrypted.pdf', | |
password='twofish') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def factorial(n): | |
if (n<=1): | |
return 1 | |
else: | |
subSolution =factorial(n-1) | |
solution = subSolution * n | |
return solution | |
factorial(3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment