Showing posts with label function. Show all posts
Showing posts with label function. Show all posts

Tuesday, March 12, 2013

Python: Remove Non-printable characters from string

Current post is on how to remove non-printable characters from string.


Need a function to remove non-printable characters in python.


Remove Non-printable function for ASCII String:


def    remove_ascii_non_printable(str)
      """Removes Non-printable chars from ASCII String"""
      return ''.join([ch for ch in str if ord(ch) > 31 and ord(ch) < 126 or ord(c) = = 9])

Remove Non-printable function for Unicode String:



def    remove_unicode_non_printable(str)
      """Removes Non-printable chars from ASCII String"""
      return ''.join([ch for ch in str if ord(ch) > 31 or ord(c) = = 9])



Appreciate your feedback via comments.

Thanks.

Thursday, July 26, 2012

Python: JOIN Function for Iterable

Current post is on how JOIN function works in Python.


Recently started working on Python and got stuck in 'JOIN' function?

Not sure how the JOIN function works?

JOIN Function for iterable in Python:


string str.join(iterable)

where,
          str = string which will be used to append the elements in iterable
          iterable = any iterable in python like list, string

          string = returns the joined string

Confused ??

Reason is we are wired to think like  object.some_function arguments but here its like argument.some_function object(s)


List JOIN in Python:


mypythonlist = ['First', 'Second']
mypytonlist.join('-')   #Incorrect Usage, it will cause AttributeError

'-'.join(mypythonlist)  #Correct Usage

# It will return 'First-Second'


String JOIN in Python:


mypythonstr = 'MyPythonString'
mypythonstr.join('-')  #Incorrect Usage

'-'.join(mypythonstr)   #Correct Usage

# It  will return 'M-y-P-y-t-h-o-n-S-t-r-i-n-g'

I hope now you understand how 'JOIN' function works in Python.


Appreciate your feedback via comments. Thanks.

Python: Converting VbScript Left-Right Function

Current post is on how to convert Left-Right Function of VBScript in Python 3.2.


Converting or migrating a VbScript project to Python?

Stuck in converting Left or Right function of VbScript in Python?


VBScript Left Function:


Left(string, Length)

where,
          string = your text string
          length = number of character from left you want to extract

Example:

myleftstring = Left("Hello from Left, noting more!!!",15);

//myleftstring will contain 'Hello from Left'.


VBScript Right Function:


Right(string, Length)


where,
          string = your text string
          length = number of character from right you want to extract


Example:


myrightstring = Right("Hello from Left, noting more!!!",15);


//myrightstring will contain ', noting more!!!'.


Python: Array Slicing:


Array slicing is done using [ : ] operator.

Example:

text = 'Hello from Python 3.2'
leftslicedtext = text[:11]

//leftslicetext will contain 'Hello from'

rightslicedtext = text[11:]

//rightslicedtext will contain ' Python 3.2'

frombehind = text[-3:]

//frombehind will contian '3.2'

Summarize:


To convert the VbScript Left / Right Function, use the array slicing feature of Python.

It is simple, clean and readable.

Appreciate your feedback via comments. Thanks.

Tuesday, February 9, 2010

What do you mean by 'Pure Virtual Function' in C++ \ VC++?

Current post is on what does 'Pure Virtual Function' mean?.


Lets examine:

What is pure virtual function ?


A pure virtual function is a virtual function which will force derived classes to override. If a class has any  pure virtual funtion(s), the class is called an "abstract class" and you can't create objects of that class.

Declaration Syntax:

return_value classname:: Func_Name( parameters ) = 0;


Example:

class AbstractBaseClass
{
     void ThisIsAPureVirtualFunction(void) = 0;
};


Why it is required?

- To specify an interface that must be provided by all classes deriving from it
- To make a class an abstract base class so that it cannot be instantiated


Can they have definition (method body)?

- Yes, they may have it.

1) If a class needs to be abstract & has no suitable candidate for virtual function, then my making destructor pure virtual & providing the definition, class is made virtual and derived class will have no issue in calling delete function

2) If a class need to provide a default behaviour but not let derived classes just inherit it "silently".


Appreciate your feedback via comments. Thanks.
Disclaimer:

The above post and all the posts in the blog are derived from facts, information, logical interpretation and logical conclusion of printed and internet materials available to me, perceived and produced by 99 gm brain of mine, which by no means always be accurate, consistent and complete.

All the posts are for personal quick reference only.

If any suggestion, correction, misinterpretation, misconception commented, which will be moderated and deleted if required, to avoid unforeseen issues.

If any trademark / copywrite issue is present, do send in a mail and appropriate tag (logo, name, website link) will be attached to the same.

Additional disclaimer will be attached wherever required in the post.