SHARE
TWEET

PYTHON2

lancernik Mar 19th, 2019 (edited) 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Reverse:
  2.     """Iterator for looping over a sequence backwards."""
  3.     def __init__(self, data):
  4.         self.data = data
  5.         self.index = len(data)
  6.  
  7.     def __iter__(self):
  8.         return self
  9.  
  10.     def next(self):
  11.         if self.index == 0:
  12.             raise StopIteration
  13.         self.index = self.index - 1
  14.         return self.data[self.index]
  15.  
  16.  
  17. rev = Reverse('spam')
  18. for char in rev:
  19.      print char
  20.  
  21. --------------------------------------------------------------------------------------------------------------
  22.  
  23. class Kabel:
  24.     def __init__(self,p1,p2):
  25.         self.p1 = p1
  26.         self.p2 = p2
  27.     def __str__(self):
  28.         return 'p1:{}, p2:{}'.format(self.p1,self.p2)
  29.     def __repr__(self):
  30.         return self.__str__()
  31.     def __add__(self, other):
  32.         return self.p1 + other.p1
  33.  
  34. k1 = Kabel(21,12)
  35. k2 = Kabel(34,43)
  36.  
  37. print(k1.__add__(k2))
  38.  
  39. print(k1)
  40.  
  41.  
  42.  
  43. --------------------------------------------------------------------------------------------------------------
  44.  
  45.  
  46. class Window():
  47.     def __init__(self,wysokosc,szerokosc):
  48.         self.wysokosc = wysokosc
  49.         self.szerokosc = szerokosc
  50.     def __str__(self):
  51.         return 'Szerokosc: {} , wysokosc: {}'.format(self.wysokosc,self.szerokosc)
  52.     def __repr__(self):
  53.         return self.__str__()
  54.     def __lt__(self,other):
  55.         return self.wysokosc * self.szerokosc < other.wysokosc*other.szerokosc
  56.  
  57. ok1 = Window(6,5)
  58. ok2 = Window(5,3)
  59.  
  60. windowslist = [ok1, ok2]
  61. print(sorted(windowslist))
  62.  
  63.  
  64.  
  65. --------------------------------------------------------------------------------------------------------------
  66.  
  67. GEN PODSTTAWOWY
  68.  
  69. def my_generator(start ,stop,step):
  70.     current_value = start
  71.     while True:
  72.         current_return = current_value
  73.         if current_value <= stop:
  74.             return
  75.         current_value -= step
  76.         yield current_return
  77.  
  78. my_gen = my_generator(start = 10, stop =2 ,step = 2)
  79.  
  80.  
  81. for value in my_gen:
  82.     print(value)
  83.    
  84.    
  85. --------------------------------------------------------------------------------------------------------------
  86.    
  87. MOJ GEN
  88.  
  89.  
  90. def my_generator(start ,stop,step):
  91.     current_value = start
  92.     while True:
  93.         if start > 0:
  94.             print("This is an odd number.")
  95.             if step % 2 == 0:
  96.                 step = step + 1
  97.                 current_return = current_value
  98.                 if current_value <= stop:
  99.                     return
  100.                 current_value -= step
  101.                 yield current_return
  102.             else:
  103.                 current_return = current_value
  104.                 if current_value <= stop:
  105.                     return
  106.                 current_value -= step
  107.                 yield current_return
  108.  
  109.  
  110.  
  111.  
  112. my_gen = my_generator(start = 30, stop =2 ,step = 4)
  113.  
  114.  
  115. for value in my_gen:
  116.     print(value)
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124. --------------------------------------------------------------------------------------------------------------
  125. --------------------------------------------------------------------------------------------------------------
  126. --------------------------------------------------------------------------------------------------------------
  127. --------------------------------------------------------------------------------------------------------------
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134. KLASY
  135.  
  136.  
  137. class TempMeasurement:
  138.     def __init__(self, temp_1, temp_2):       #<-- Konstruktorem wprowadzamy dane do klasy!!!
  139.         self.temp_1 = temp_1
  140.         self.temp_2 = temp_2
  141.     def temp_combinations(self):
  142.         output_list = []
  143.         for i in range(self.temp_1+1):
  144.             for j in range(self.temp_2 + 1):
  145.                 output_list.append((i,j))                #<-- W taki sposob mozna doadawc krotke do listy
  146.         return output_list
  147.     def __str__(self):              #<-- Przy printowaniu funkcji wyskoczy to co tu
  148.         return "Temperatura 1: " + str(self.temp_1) + "Temperatura 2: " + str(self.temp_2)
  149.  
  150. temp_measurement_1 = TempMeasurement(2,3)
  151.  
  152. print(temp_measurement_1)
  153. print(temp_measurement_1.temp_combinations())
  154.  
  155.  
  156.  
  157.  
  158.  
  159. --------------------------------------------------------------------------------------------------------------
  160.  
  161.  
  162.  
  163. class Measurment:
  164.     def __init__(self, p1, p2):
  165.         self.p1 = p1
  166.         self.p2 = p2
  167.     def __str__(self):
  168.         return str(self.p1) + " " + str(self.p2)
  169.     def __repr__(self):
  170.         return self.__str__()
  171.     def __lt__(self,other):           #<-- Definiuje w jaki sposob sort ma porownywac obiekty
  172.         return self.p2 < other.p2
  173.  
  174.  
  175. measure1 = Measurment(1, 5)
  176. measure2 = Measurment(5, 4)
  177. mesaures = [measure1, measure2]
  178.  
  179. print(sorted(mesaures))
  180.  
  181.  
  182.  
  183.  
  184.  
  185. --------------------------------------------------------------------------------------------------------------
  186. --------------------------------------------------------------------------------------------------------------
  187. --------------------------------------------------------------------------------------------------------------
  188.  
  189.  
  190.  
  191. GENERATORY
  192.  
  193.  
  194.  
  195. def counter(start, stop, step):
  196.     current_value = start
  197.     while True:
  198.         current_return = current_value
  199.         if current_value >= stop:
  200.             return                  #<-- Return pozwala na wyjscie z nieskonczonej pętli
  201.         current_value += step
  202.         yield current_return
  203.  
  204.  
  205. for i in counter(0, 10,2):
  206.     print(i)
  207.  
  208.  
  209.  
  210.  
  211.  
  212. --------------------------------------------------------------------------------------------------------------
  213.  
  214.  
  215.  
  216.  
  217. def my_generator(start ,stop,step):
  218.     current_value = start
  219.     while True:
  220.         current_return = current_value
  221.         if current_value <= stop:
  222.             return
  223.         current_value -= step
  224.         yield current_return
  225.  
  226. my_gen = my_generator(start = 10, stop =2 ,step = 2)
  227.  
  228. for value in my_gen:
  229.     print(value)
  230.  
  231.  
  232.  
  233.  
  234. --------------------------------------------------------------------------------------------------------------
  235.  
  236.  
  237. FUNKCJE WEW.
  238.  
  239. list = [1,2,3,4,5,6,7]
  240. def my_multiply(number):
  241.     def inner_func(list):
  242.         return [number * item for item in list]
  243.     return inner_func
  244.  
  245. number_1 = 2
  246. number_2 = 5
  247.  
  248. list = [1,4,5,6]
  249.  
  250. inner_1 = my_multiply(number_1)
  251.  
  252. print(inner_1(list))
  253.  
  254.  
  255. --------------------------------------------------------------------------------------------------------------
  256.  
  257.  
  258.  
  259.  
  260. ef multiply_my(list):
  261.     def inner(number):
  262.         return [number * item for item in list]
  263.     return inner
  264.  
  265. list = [1,5,2,5,2]
  266. test = multiply_my(list)
  267. print(test(5))
  268.  
  269.  
  270.  
  271. --------------------------------------------------------------------------------------------------------------
  272.  
  273.  
  274.  
  275.  
  276. DEKORATORY
  277.  
  278.  
  279.  
  280. def my_decorator(func):
  281.     def wrapper(arg):
  282.         print("Hello Decorator")
  283.         return func(arg)
  284.     return wrapper
  285.  
  286.  
  287. @my_decorator
  288. def DecoratorTest(arg):
  289.     def print_me():
  290.         print(arg)
  291.     return print_me()
  292.  
  293.  
  294. DecoratorTest("Ten element bedzie dekorowany")
  295.  
  296.  
  297.  
  298.  
  299.  
  300. --------------------------------------------------------------------------------------------------------------
  301.  
  302.  
  303.  
  304.  
  305.  
  306. def my_decorator(func):
  307.     def wrapper():
  308.         print("Hello Decorator")
  309.         return func()
  310.     return wrapper
  311.  
  312. @my_decorator
  313. def DecoratorTest():
  314.     def print_me():
  315.         print("Test")
  316.     return print_me()
  317.  
  318. DecoratorTest("Ten element bedzie dekorowany")
  319.  
  320.  
  321.  
  322.  
  323. --------------------------------------------------------------------------------------------------------------
  324.  
  325.  
  326. XDDDDDDDDDDDDDDDDDDDDDDDDDDDD
  327.  
  328.  
  329. def zad9(input_dict, input_list):
  330.     for item in input_list:
  331.         if item not in input_dict.keys():
  332.             raise ValueError("Key " + str(item) + " does not exist.")
  333.  
  334. dict_1 = {'a': 5, 'b': 7, 'c': 4}
  335. dict_2 = {'a': 5, 'b': 7, 'd': 5}
  336. input_list = {'a', 'b', 'c'}
  337.  
  338. try:
  339.     zad9(dict_2, input_list)
  340.     print("Ok")
  341. except ValueError as e:
  342.     print("ValueError")
  343.     print(e)
  344. except Exception as e:
  345.     print(e)
  346.  
  347.  
  348. def our_decorator(input_function):
  349.     def inner_function(*args):
  350.         if len(args[0]) != len(args[1]):
  351.             raise ValueError("Vectors must be the same length.")
  352.         return input_function(*args)
  353.     return inner_function
  354.  
  355.  
  356. class VectorExample():
  357.     def __init__(self, input_list):
  358.         self.vector = input_list
  359.     def __str__(self):
  360.         return str(self.vector)
  361.     @our_decorator
  362.     def foreach(self, other, input_function):
  363.         return VectorExample([input_function(self.vector[i], other.vector[i]) for i in range(len(self.vector))])
  364.     def __add__(self, other):
  365.         return self.foreach(other, lambda a, b: a + b)
  366.     def __sub__(self, other):
  367.         return self.foreach(other, lambda a, b: a - b)
  368.     def __mul__(self, other):
  369.         return self.foreach(other, lambda a, b: a * b)
  370.     def __truediv__(self, other):
  371.         return self.foreach(other, lambda a, b: a / b)
  372.     def __len__(self):
  373.         return len(self.vector)
  374.  
  375.  
  376. vector_1 = VectorExample([1, 2, 3, 4])
  377. vector_2 = VectorExample([-1, 3, -2, 1])
  378. vector_3 = VectorExample([1, 2, 3, 0])
  379. vector_4 = VectorExample([1, 2, 3])
  380.  
  381. print(vector_1)
  382. print(vector_2)
  383. print(vector_1 + vector_2)
  384. print(vector_1 - vector_2)
  385. print(vector_1 * vector_2)
  386. print(vector_1 / vector_2)
  387.  
  388.  
  389. try:
  390.     print(vector_1 / vector_2)
  391.     print("Success")
  392. except Exception as e:
  393.     print(e)
  394.  
  395. print(vector_1 + vector_4)
  396.  
  397. print("The End")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top