Rajendra Dharmkar has Published 537 Articles

How you can get a true/false from a Python regular expressions?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 16-Jun-2020 11:43:03

When you use match and search methods of module re, if there is a match, it has bool value of True and if there is no match, you get None that has a bool value of False.Match objects are always true, and None is returned if there is no match>>> ... Read More

How to encode custom python objects as BSON with Pymongo?

Rajendra Dharmkar

Rajendra Dharmkar

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

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():       ... Read More

How to create Python objects based on an XML file?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 16-Jun-2020 11:25:01

UntangleWe can use untangle to create Python objects based on an XML fileuntangle is a simple library which takes an XML document and returns a Python object which mirrors the nodes and attributes in its structure.For example, an XML file like this −     can be loaded ... Read More

How to get the return value from a function in a class in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 16-Jun-2020 11:04:53

The following code shows how to get return value from a function in a Python classExampleclass Score():     def __init__(self):         self.score = 0         self.num_enemies = 5         self.num_lives = 3     def setScore(self, num):     ... Read More

How to attach a C method to existing Python class?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 16-Jun-2020 11:00:12

This method shows how to define a new Python class from a C extension module. The methods of the class are implemented in C, but the class can still be instantiated, subclassed, and extended from Python. The same technique with inheritance can also be used to extend an existing Python ... Read More

Explain Inheritance vs Instantiation for Python classes.

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 16-Jun-2020 09:17:52

InheritanceBeing an Object Oriented language, Python supports inheritance, it even supports multiple inheritance. Classes can inherit from other classes. A class can inherit attributes and behaviour methods from another class, called the superclass. A class which inherits from a superclass is called a subclass, also called heir class or child ... Read More

How not to match a character after repetition in Python Regex?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 16-Jun-2020 09:08:00

^  # Beginning of string(?! # Do not match if|2{1, 2}5)# Or there is one or two 2s followed by a 5.* # Anything else is acceptedExampleThe following code fulfils the conditions in given questionimport re foo = '2249678' foo2 = '2259678' result = re.findall(r'^(?!2{1, 2}5).*', foo) result2 = re.findall(r'^(?!2{1, 2}5).*', foo2) ... Read More

How does class variables function in multi-inheritance Python classes?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 16-Jun-2020 09:04:20

A class can be derived from more than one base classes in Python. This is called multiple inheritance.In multiple inheritance, the features of all the base classes are inherited into the derived class. The syntax for multiple inheritance is similar to single inheritance.class Super1:     pass class Super2:   ... Read More

How do I declare a global variable in Python class?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 16-Jun-2020 08:40:52

A global variable is a variable with global scope, meaning that it is visible and accessible throughout the program, unless shadowed. The set of all global variables is known as the global environment or global scope of the program. We declare a variable global by using the keyword global before a ... Read More

How I can create Python class from JSON object?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 16-Jun-2020 08:38:28

We can use python-jsonschema-objects which is built on top of jsonschema.The python-jsonschema-objects provide an automatic class-based binding to JSON schemas for use in Python.We have a sample json schema as followsschema = '''{     "title": "Example Schema",     "type": "object",     "properties": {         ... Read More

Advertisements