
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 10476 Articles for Python

449 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

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

984 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

814 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

410 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

861 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

15K+ Views
The following code shows how to get return value from a function in a Python class.Exampleclass Score(): def __init__(self): self.score = 0 self.num_enemies = 5 self.num_lives = 3 def setScore(self, num): self.score = num def getScore(self): return self.score def getEnemies(self): return self.num_enemies def getLives(self): return self.num_lives s = Score() s.setScore(9) print s.getScore() print s.getEnemies() print s.getLives()Output9 5 3

16K+ Views
In Python, we can return an object from a function, using the return keyword, just like any other variable. The statements after the return will not be executed. The return keyword cannot be used outside the function. If the function has a return statement without any expression, then the special value None is returned. In the following example, the function returned the sum of two numbers - def Sum(a, b): return a+b my_var1 = 23 my_var2 = 105 result_1 = Sum(my_var1, my_var2) # function call print(result_1) Following is an output of ... Read More

197 Views
By default, all .NET objects are reference types and their equality and hash code is determined by their memory address. Additionally, assigning a variable to an existing object just makes it point to that address in memory, so there is no costly copying occurring. It appears that this is true for python objects as well to certain extent.Properties of Python objects: All python objects havea unique identity (an integer, returned by id(x)); a type (returned by type(x))You cannot change the identity; You cannot change the type.Some objects allow you to change their content (without changing the identity or the type, that is).Some ... Read More

309 Views
We have to work with large size files in Python. If we use the large files, they occupy more space, and it isn't easy to handle the large files too. In such cases, we have a module in Python to work with the large files. The module we can use to handle large files is zipfile. By using this module, we can compress a large file into smaller files in Python. The following is the process that we have to follow for compressing the large files. ... Read More