- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 10784 Articles for Python
![Gireesha Devara](https://www.tutorialspoint.com/assets/profiles/397483/profile/60_1063327-1637125522.jpg)
1K+ Views
XML (Extensible Markup Language), which is a markup−language that is used to structure, store, and transfer data between systems. At some point we need to read/write the XML data using the Python language. By using the untangle library we can create Python objects based on an XML file. The untangle is a small Python library which converts an XML document to a Python object. The untangle has a very simple API. We just need to call the parse() function to get a Python object. Syntax untangle.parse(filename, **parser_features) Parameters Filename: it can be a XML string, a XML filename, ... Read More
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
112 Views
Python uses two techniques to clean up garbage. One is reference counting, which affects all objects but which can't clean up objects that directly or indirectly refer to each other. That's where the actual garbage collector comes in: python has the gc module, which searches for cyclic references in objects it knows about. Only objects that can potentially be part of a reference cycle participate in the cyclic gc. So, for example, lists do, but strings do not; strings don't reference any other objects.All Python classes and their instances automatically get tracked by the cyclic gc. Types defined in C ... Read More
![Gireesha Devara](https://www.tutorialspoint.com/assets/profiles/397483/profile/60_1063327-1637125522.jpg)
274 Views
In python there is no built−in or normal function that acts as an object inspector. But we can use functions like type(), help(), dir(), vars() or modules like inspect are used to find the attributes, properties and methods of any object. Also we have other functions like id(), getattr(), hasattr(), globals(), locals(), callable() are useful in looking inside an object to know its attributes and methods. Here we will inspect the objects using some built−in functions. Before that we will create a simple python class and its objects, to refer throughout this article. Following is the syntax to defing ... Read More
![Gireesha Devara](https://www.tutorialspoint.com/assets/profiles/397483/profile/60_1063327-1637125522.jpg)
268 Views
To wrap existing C or C++ functionality in Python, there are number of options available, which are: Manual wrapping using PyMethodDef and Py_InitModule, SWIG, Pyrex, ctypes, SIP, Boost.Python, and pybind1. Using the SWIG Module Let’s take a C function and then tune it to python using SWIG. The SWIG stands for “Simple Wrapper Interface Generator”, and it is capable of wrapping C in a large variety of languages like python, PHP, TCL etc. Example Consider simple factorial function fact() in example.c file. /* File : example.c */ #include // calculate factorial int fact(int n) ... Read More
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
285 Views
To encode custom python objects as BSON with Pymongo, you have to write a SONManipulator. From the docs:SONManipulator instances allow you to specify transformations to be applied automatically by PyMongo.from pymongo.son_manipulator import SONManipulator class Transform(SONManipulator): def transform_incoming(self, son, collection): for (key, value) in son.items(): if isinstance(value, Custom): son[key] = encode_custom(value) elif isinstance(value, dict): # Make sure we recurse into sub-docs son[key] = self.transform_incoming(value, collection) return son def transform_outgoing(self, son, collection): for (key, value) in son.items(): ... Read More
![Gireesha Devara](https://www.tutorialspoint.com/assets/profiles/397483/profile/60_1063327-1637125522.jpg)
21K+ Views
Python is an object-oriented programming language, here attributes are known as properties of an object. By using different methods, we can check if an object has an attribute or not. To check if an object contains a particular attribute then we can use hasattr() method and getattr() method. Or if we want to get all existing attributes then we can use the dir() method. (Learn more about Python directories: Python Directories Tutorial) Initially create a dummy Python class with two attributes then assign it to an object, and it will refer throughout this article. class DummyClass(): ... Read More
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
749 Views
Assuming that a MySQL database named 'test' is present on the server and a table named employee is also created. Let the table have five fields fname, lname, age, gender, and salary.Suppose we want to insert a tuple object containing data of a record defined as follows into the Msql database.t1=('Steven', 'Assange', 21, 'M', 2001)To establish an interface between MySQL and Python 3, you need to install the PyMySQL module. Then you can set up the connection using the following statementsimport PyMySQL # Open database connection db = PyMySQL.connect("localhost", "root", "", "test" ) # prepare a cursor object using cursor() ... Read More
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
577 Views
You can use the pymongo library in Python to connect to a MongoDB database and use it to insert, update, delete, etc objects in Python. The library supports Python datetime objects out of the box and you dont need to do anything special to insert dates in Mongo using PyMongo. For example, Examplefrom pymongo import MongoClient # This will try to connect to MongoDB on the default port and host client = MongoClient() db = client.test_database # Insert the given dictionary to the objects collection: result = db.objects.insert_one({"last_modified": datetime.datetime.utcnow()}) print("Object inserted!")OutputThis will give the output −Object inserted!Note − Always use ... Read More
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
243 Views
We need sometimes to compress Python objects (list, dictionary, string, etc) before saving them to cache and decompress after reading from cache.Firstly we need to be sure we need to compress the objects. We should check if the data structures/objects are too big just to fit uncompressed in the cache. There is going to be an overhead for compression/decompression, that we have to tradeoff with the gains made by caching in the first place.If we really need compression, then we probably want to use zlib.If we are going to use zlib, we might want to experiment with the different compression ... Read More
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
686 Views
Here is an example in which a simple Python object is wrapped and embedded. We are using .c for this, c++ has similar steps −class PyClass(object): def __init__(self): self.data = [] def add(self, val): self.data.append(val) def __str__(self): return "Data: " + str(self.data) cdef public object createPyClass(): return PyClass() cdef public void addData(object p, int val): p.add(val) cdef public char* printCls(object p): return bytes(str(p), encoding = 'utf-8')We compile with cython pycls.pyx (use --cplus for c++) to generate ... Read More