Rajendra Dharmkar has Published 189 Articles

How can I import modules for a Python Azure Function?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 17-Feb-2020 07:41:36

1K+ Views

As of writing this, Python support for Azure Functions is experimental. So right now there is no way to directly get a module from a package manager to be installed on your instance. You'll need to bring your own modules with code. No modules are available by default on Azure ... Read More

How to convert byte literals to python strings?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Feb-2020 10:32:41

480 Views

To convert byte literals to Python strings, you need to decode the bytes. It can be done using the decode method on the bytes object.  example>>> b"abcde".decode("utf-8") u'abcde'You can also map bytes to chr if the bytes represent ASCII encoding as follows −bytes = [112, 52, 52] print("".join(map(chr, bytes)))Outputp44Read More

How to use Python object in C++?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 10-Feb-2020 10:49:28

862 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 ... Read More

How to insert a Python object in MySQL?

Rajendra Dharmkar

Rajendra Dharmkar

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

987 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 ... Read More

How to match tab and newline but not space using Python regular expression?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 19-Dec-2019 09:03:45

2K+ Views

The following code matches tab and newline but not space from given string using regex.Exampleimport re print re.findall(r"[\t]","""I find     Tutorialspoint useful""")OutputThis gives the output['']

How to remove tabs and newlines using Python regular expression?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 19-Dec-2019 09:02:07

960 Views

The following code removes tabs and newlines from given stringExampleimport re print re.sub(r"\s+", " ", """I find Tutorialspoint helpful""")OutputThis gives outputI find Tutorialspoint helpful

How to compare two strings using regex in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 19-Dec-2019 08:49:48

2K+ Views

We can compare given strings using the following codeExampleimport re s1 = 'Pink Forest' s2 = 'Pink Forrest' if bool(re.search(s1,s2))==True:    print 'Strings match' else:    print 'Strings do not match'OutputThis gives the outputStrings do not match

How to divide a string by line break or period with Python regular expressions?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 16-Dec-2019 08:57:39

383 Views

The following code splits given string by a period and a line break as followsExampleimport re s = """Hi. It's nice meeting you. My name is Jason.""" result = re.findall(r'[^\s\.][^\.]+', s) print resultOutputThis gives the following output['Hi', "It's nice meeting you", 'My name is Jason']

How to force write of file with filedescriptor fd to disk using Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Dec-2019 09:53:06

386 Views

You have to use the fdatasync(fd) function to force write of file with filedescriptor fd to disk. It does not force update of metadata. Also note that this is only available on Unix.A more cross platform solution would be to use fsync(fd) as it force write of file with filedescriptor ... Read More

How to change the user and group permissions for a directory using Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Dec-2019 07:14:14

608 Views

You can change the owner of a file or a directory using the pwd, grp and os modules. The uid module is used to get the uid from user name, grp to get gid group name string and os to change the owner:Exampleimport pwd import grp import os uid = ... Read More

Advertisements