Skip to content

Instantly share code, notes, and snippets.

@EkremDincel
EkremDincel / builtinmodules.py
Created January 24, 2020 12:34
Python standard library attributes
import collections
import importlib
import inspect
import sys
from stdlib_list import stdlib_list
MODULES = stdlib_list("3.8")
def gettype(x):
if inspect.ismodule(x): return "module"
@EkremDincel
EkremDincel / urllibexample.py
Last active February 10, 2020 19:42
urllib code example
from urllib.request import urlopen, Request
from bs4 import BeautifulSoup
rootlink = 'https://www.transfermarkt.pl' # root link of website
link = 'https://www.transfermarkt.pl/schnellsuche/ergebnis/schnellsuche?query=' # our link, as you know
def create_request(url): # let me explain it below
req = Request( # we are creating a Request instance
url, # giving our 'url'
data=None, # this is not important for now :D
@EkremDincel
EkremDincel / set vs list.py
Created February 23, 2020 17:47
Python comparison of set and list performance
from timeit import timeit as _timeit
uzunluk = 10000
orta = int(uzunluk / 2)
s_number = uzunluk
l_number = uzunluk
timeit = lambda code, number = 1000: _timeit(code, globals = globals(), number = number)
@EkremDincel
EkremDincel / algoritmalar.py
Last active March 1, 2020 15:53
İlginç soruları çözmek için kullanılabilecek fonksiyonlar.
import math
##############
def ebob(x,y):
while y:
r, x = x, y
y=r%y
return x
@EkremDincel
EkremDincel / analysts.py
Last active April 10, 2020 15:37
An example tool for analyzing python class hierarchy statically
import ast
with open("module.py", "r") as f: # we will analysis this file
data = f.read()
tree = ast.parse(data)
classes = {} # {class: [parents]}
for i in tree.body:
@EkremDincel
EkremDincel / microstream.py
Last active July 15, 2020 00:45
A little TCP socket interface for Python.
import json
import socket
from struct import Struct, calcsize
from select import select
__all__ = ("Server", "Client", "localhostname", "localhost", "LAN", "WAN")
_int = "H" # 0 <= number <= 65535 == 2 ** 16 -1
_size = calcsize(_int)
_struct = Struct("!" + _int)
@EkremDincel
EkremDincel / sayıların_okunuşu.py
Created June 1, 2020 16:51
Python'da Türkçe tam sayıların okunuşunu hesaplayan fonksiyon örneği
digit = ['sıfır', 'bir', 'iki', 'üç', 'dört', 'beş', 'altı', 'yedi', 'sekiz', 'dokuz']
digit2 = ['', 'on', 'yirmi', 'otuz', 'kırk', 'elli', 'altmış', 'yetmiş', 'seksen', 'doksan']
digit3 = ['milyon', 'milyar', 'trilyon', 'katrilyon', 'kentilyon', 'sekstilyon', 'septilyon', 'oktilyon', 'nonilyon', 'desilyon', 'andesilyon', 'dodesilyon', 'tredesilyon', 'katordesilyon', 'kendesilyon', 'seksdesilyon', 'septendesilyon', 'oktodesilyon', 'novemdesilyon', 'vicintilyon', 'anvicintilyon', 'dovicintilyon', 'trevicintilyon', 'katorvicintilyon', 'kenkavicintilyon', 'sesvicintilyon', 'septemvicintilyon', 'oktovicintilyon', 'novemvicintilyon', 'tricintilyon', 'antricintilyon']
onluk = lambda i: (digit[i] if i<10 else digit2[i // 10] + " " + digit[i % 10]) if i%10 else digit2[i // 10]
yüzlük = lambda i: onluk(i) if i < 100 else ("yüz " + onluk(i - 100) if i-100 else "yüz") if i < 200 else digit[i // 100] + " yüz " + onluk(i % 100) if i%100 else digit[i // 100] + " yüz"
binlik = lambda i: ("bin" if i == 1 else yüzlük(i) + "
@EkremDincel
EkremDincel / enum.py
Created June 14, 2020 13:52
Pythonic Enum example
class MyEnum():
(ONE,
TWO,
THREE,
FOUR,
FIVE) = range(5)
from functools import reduce, partial
import itertools as it
from operator import itemgetter, methodcaller
def reversed_partial(f, *args, **kwargs):
def wrapper(*_args, **_kwargs):
f(*_args, *args, **_kwargs, **kwargs)
return wrapper
def unpack(iterable):
@EkremDincel
EkremDincel / enum_meta.py
Created October 7, 2020 12:06
Implementing enumerations in Python using metaclasses.
class EnumMetaDict(dict):
def __init__(self):
self.n = -1
def __getitem__(self, item):
if item.isupper():
self.n += 1
self[item] = self.n
return self.n