Programming Articles - Page 3250 of 3366

Python3 - Why loop doesn\'t work?

Akshitha Mote
Updated on 29-May-2025 15:56:17

229 Views

In Python, using loops, we can execute a statement or a group of statements multiple times. Usually, the loop works for all the conditions, but there are certain scenarios where the functionality of loops is affected. In this article, we will understand those scenarios with examples. When sleep() method is used inside Loop The sleep() method is defined in the time module and used to suspend or stop the execution of the program for a specified number of seconds. When the sleep() method is placed inside a loop, it suspends the execution of each iteration for the given time. After ... Read More

How to create Python objects based on an XML file?

Gireesha Devara
Updated on 24-Aug-2023 12:45:36

2K+ 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

Do you think garbage collector can track all the Python objects?

Akshitha Mote
Updated on 29-May-2025 13:07:10

199 Views

The garbage collector doesn't track (and collect) all the objects; it can only track objects that are unreachable (have a reference count of zero), and the objects that are involved in circular references. What is a garbage collector? The garbage collector is an automatic (implicit) process that handles memory allocation and deallocation, ensuring efficient use of memory. We can interact with the garbage collector in Python explicitly and modify its behavior, using the gc module; it is a built-in module in Python for garbage collection.  By default, it is turned ON; you may turn it off if you are sure ... Read More

Is there any Python object inspector?

Gireesha Devara
Updated on 23-Aug-2023 19:09:55

471 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

How to wrap python object in C/C++?

Gireesha Devara
Updated on 24-Aug-2023 16:01:06

556 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

How to encode custom python objects as BSON with Pymongo?

Rajendra Dharmkar
Updated on 16-Jun-2020 11:29:35

458 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

How to know if an object has an attribute in Python?

Gireesha Devara
Updated on 29-Aug-2023 03:41:46

23K+ 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

How to insert a Python object in MySQL?

Rajendra Dharmkar
Updated on 28-Jan-2020 06:11:54

995 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

How to insert a Python object in Mongodb?

SaiKrishna Tavva
Updated on 18-Dec-2024 19:23:27

832 Views

By using pymongo library, we can insert a Python object into MongoDB, which is a widely used MongoDB driver for Python. To insert a Python object represented as a dictionary, we have to establish the setup for a MongoDB client and connect to a database and collection. Before starting, ensure you have installed Python 3 (along with PIP) and MongoDB. The following are the steps for inserting a Python object in MongoDB. Install PyMongo ... Read More

How to compress Python objects before saving to cache?

Rajendra Dharmkar
Updated on 30-Jul-2019 22:30:21

418 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

Advertisements