-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
203 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
def isint(f): | ||
def newfun(*args): | ||
for i in args: | ||
if type(i) != int: | ||
raise TypeError | ||
return f | ||
|
||
|
||
def genf(f): | ||
def newfun(*args): | ||
print(">", *args) | ||
res = f(*args) | ||
print("<", res) | ||
return res | ||
return newfun | ||
|
||
@isint | ||
@genf | ||
def fun(a, b): | ||
return a + b * 2 | ||
newf = genf(fun) | ||
|
||
|
||
try: | ||
print(fun(2, 3)) | ||
except: | ||
print('TypeError') |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
def istype(tpe): | ||
def d(f): | ||
def newfun(*args): | ||
for i in args: | ||
if not isinstance(i, tpe): | ||
raise TypeError | ||
return f(*args) | ||
return d | ||
|
||
|
||
def genf(f): | ||
def newfun(*args): | ||
print(">", *args) | ||
res = f(*args) | ||
print("<", res) | ||
return res | ||
return newfun | ||
|
||
@istype(str) | ||
@genf | ||
def fun(a, b): | ||
return a + b * 2 | ||
newf = genf(fun) | ||
|
||
|
||
try: | ||
print(fun(2, 3)) | ||
except: | ||
print('TypeError') |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Sender: | ||
flag = True | ||
|
||
@classmethod | ||
def report(self): | ||
if self.flag: | ||
print('Greetings') | ||
self.flag = False | ||
else: | ||
print('Get away') | ||
|
||
class Asker: | ||
@staticmethod | ||
def askall(lst): | ||
for i in lst: | ||
i.report() | ||
|
||
|
||
b = Asker() | ||
lst = [Sender() for i in range(10)] | ||
b.askall(lst) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class Dsc: | ||
|
||
def __get__(self, obj, cls): | ||
print(f"Get from {cls}:{repr(obj)}") | ||
return obj._value | ||
|
||
def __set__(self, obj, val): | ||
print(f"Set in {repr(obj)} to {val}") | ||
obj._value = val | ||
|
||
def __delete__(self, obj): | ||
print(f"Delete from {repr(obj)}") | ||
obj._value = None | ||
|
||
class C: | ||
data = Dsc() | ||
|
||
def __init__(self, name): | ||
self.data = name | ||
|
||
def __str__(self): | ||
return f"<{self.data}>" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
class Counter(): | ||
def __init__(self): | ||
self.counter = 0 | ||
|
||
def __get__(self, obj, cls): | ||
return self.counter | ||
|
||
def __set__(self, obj, val): | ||
self.counter = val | ||
|
||
def __delete__(self, obj): | ||
self.counter -= 1 | ||
|
||
|
||
class C: | ||
counter = Counter() | ||
def __init__(self): | ||
self.counter += 1 | ||
def __del__(self): | ||
self.counter -= 1 | ||
c = C() | ||
print(c.counter) | ||
d = C() | ||
print(d.counter) | ||
del c | ||
print(d.counter) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class C: | ||
@property | ||
def age(self): | ||
if self._v == 42: | ||
print("secret") | ||
return -1 | ||
return self._v | ||
|
||
@age.setter | ||
def age(self, value): | ||
if value > 128: | ||
raise ValueError | ||
self._v = value | ||
|
||
@x.getter | ||
def x(self, value): | ||
self._x = value | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import sys | ||
from string import ascii_letters | ||
from pympler.asizeof import asizeof | ||
class Trad: | ||
def __init__(self): | ||
for attr in ascii_letters: | ||
setattr(self, attr, attr) | ||
|
||
class Slotter: | ||
__slots__ = tuple(ascii_letters) | ||
|
||
def __init__(self): | ||
for attr in ascii_letters: | ||
setattr(self, attr, attr) | ||
|
||
a = Slotter() | ||
print(sys.getsizeof(a)) | ||
print('a', asizeof(a)) | ||
a = Trad() | ||
print(sys.getsizeof(a)) | ||
print('a', asizeof(a)) | ||
|
||
a = [Trad() for i in range(1000)] | ||
print(sys.getsizeof(a)) | ||
print('a', asizeof(a)) | ||
|
||
a = [Slotter() for i in range(1000)] | ||
print(sys.getsizeof(a)) | ||
print('a', asizeof(a)) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
class Dumper(): | ||
def __init__(self, function): | ||
self.function = function | ||
|
||
def __call__(self, *args, **kwargs): | ||
def newfun(*args): | ||
if not all(isinstance(arg, self.function) for arg in args): | ||
raise TypeError | ||
return fun(*args) | ||
return newfun | ||
|
||
def istype(tpe): | ||
def d(f): | ||
def newfun(*args): | ||
for i in args: | ||
if not isinstance(i, tpe): | ||
raise TypeError | ||
return f(*args) | ||
return d | ||
|
||
@istype(int) | ||
def fun(a, b): | ||
return a + b * 2 | ||
|
||
|
||
a = Dumper(fun(2, 3)) | ||
try: | ||
print(fun(2, 3)) | ||
except: | ||
print('TypeError') |