PYTHON 1

¡Supera tus tareas y exámenes ahora con Quizwiz!

SERVER-SOCKET METHODS *s.bind()* - this method binds the address (hostname port number pair) to socket. The server is in the active state. *s.listen()* - this method sets up and starts a TCP listener *s.accept()* - passively accept TCP clients connection, waiting until the connection arrives (blocking)

s = socket.socket(socket_family, socket_type, protocol=0) *socket_family* - AF_UNIX or AF_INET *socket_type* -either SOCK_STREAM or SOCK_DGRAM

DECORATORS

*@property* - by just adding this decorator before a function, we can access the function like an attribute (print(emp_1.fullname) instead of print(imp_1.fullname())) *@setter* - @fullname.setter def fullname(self, name): first, last = name.split(' ') self.first = first self.last = last emp_1 = Employee('John', 'Smith') emp_1.fullname = 'Corey Schafer' *@deleter* - @fullname.deleter def fullname(self): self.first = None self.last = None emp_1 = Employee('John', 'Smith') del emp_1.fullname

If we have many python packages installed in our system and we want to pass the list of the installed packages with their versions to our colleague to be installed, we can use *pip freeze* command

*pip freeze > requirements.txt* now if we have this requirements file and wonna install all the packages within it *pip install -r requirements.txt*

*pip python packet manager*

*pip help* -- > all commands with their options *pip help install* --> options for install *pip search Pympler* -- > returns package name *pip install Pympler* -- > install the package *pip list* -- > shows all the packages installed *pip uninstall Pympler* -- > uninstall the package *pip list -o* -- > to check which package is outdate *pip install -U packagename* -- > to upgrade *pip list --outdated* -- > to upgrade outdateds

CLIENT-SOCKET METHODS *s.connect()* - this method actively initiates TCP connection

GENERAL-SOCKET METHODS (common) *s.recv()* - this method transmits a TCP message *s.send()* - this method sends a TCP message *s.recvfrom()* - this method received a UDP messa *s.sendto()* - this method sends a UDP message *s.close()* - this method closes the socket *socketgethostname()* - returns the hostname

*CGI executables*

HTTP server can be configured such that when a particular file is requested, it is not sent back to the browser, if it is an executable file, it is then executes and the result is sent back. In http.conf (AddHandle) configuration file, it is possible Python, C++ scripts make to be treated as CGI files.

*Lambda Functions*

Syntax of Lambda Function *lambda arguments: expression* Lambda functions can have any number of arguments but only one expression. The expression is evaluated and returned. Lambda functions can be used wherever function objects are required. # Program to show the use of lambda functions double = lambda x: x * 2 # Output: 10 print(double(5))

Execution object

Two kinds of things can happen between objects 1. - You can send a message to change the attribute of the other object. 2. - You can ask the other object to do something for you and send the result back.

*simple server*

Use the *socket()* function in *socket* module to create a socket object socket objects is used to call other functions to set up a socket server now call the bind( hostname, port) function to specify a port for your service on the given host Next, call the *accept* method of the returned object This method waits until the client connects on the port you specified, then returns the *connection* object that represents the connection to the client

use DIR function to see the members of a class

class DemoClass: pass my_class = DemoClass() print(dir(DemoClass)) ['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', __str__, __setattr__ ......] print(dir(my_class)) the output is the same list of members of like in the class case --> ['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', __str__, __setattr__ ......]

*simple client*

client program opens a connection to a given port with the specified host name create a socket client using Python's socket module function the socket.connect(hostname, port) opens a TCP connection to hostname on the port once you have a socket open, you can read it like any other IO object. When done, remember to close it, as you would close any file.

self is the ID of the object

if you want all variables defined in the class to be accessed by the methods of that class, in the __init__() method those variable should be defined with *self* (e.g., self.account = 0) this variables are called instance variables the variables defined in __init__() method without *self* are called local variables (e.g., account = 0) and can be used only in that method

Each object has an ID, TYPE and VALUE

my_string = ¨davit¨ print (id(my_string)) print(type(my_string)) print(my_string) string class 34343543- ID <class 'str'>- TYPE davit - VALUE

Dunder

print(1+2) = print(int.__add__(1,2)) print('a' + 'b') = print(string.__add__('a', 'b')) if we wonna add pays of two Employee object together # we create our own ___add__ method def __add__(self, other): return self.pay + other.pay print(emp1 + emp2)

*regular methods* by default pass the instance name as an argument -- > def apply_raise(self): *class methods* by default pass the class name as an argument -- > def set_raise_amt(cls, amount): *static method* by default pass nothing as an argument -- > they behave just like a regular functions except we include them in the classes since they have some logical connections with the class

raise_amt = 1.04 def apply_raise(self): self.pay = int(self.pay * self.raise_amt) @classmethod def set_raise_amt(cls, amount): cls.raise_amt = amount @staticmethod def is_working(day): if day.weekday() == 5 or day.weekday() == 6: return False return True


Conjuntos de estudio relacionados

Domain 4: IS Operations, Maintenance & Service Management

View Set

Chapter 9 Lab Textbook Reading and Reading Questions

View Set

Chapter 29- Quiz #2 & Lecture Material

View Set

Health Law Final Quiz (RHIA/RHIT) Quiz 10.1

View Set