Python shlex class














































Python shlex class



                                                               Python Shlex class
Hello,This is Rohit Kumar.In this article I will talk about shlex class in the shlex module. 
The shlex module defines the following class:
class shlex.shlex(instream=None, infile=None, posix=False, punctuation_chars=False)

We Know that  a shlex instance or subclass instance is a lexical analyzer object. The initialization argument, if present, specifies where to read characters from. It must be a file-/stream-like object with read() and readline() methods, or a string. If no argument is given, input will be taken from sys.stdin. The second optional argument is a filename string, which sets the initial value of the infile attribute. If the instream argument is omitted or equal to sys.stdin, this second argument defaults to "stdin". The posix argument defines the operational mode: when posix is not true (default), the shlex instance will operate in compatibility mode. When operating in POSIX mode, shlex will try to be as close as possible to the POSIX shell parsing rules. The punctuation_chars argument provides a way to make the behaviour even closer to how real shells parse. This can take a number of values: the default value, False, preserves the behaviour seen under Python 3.5 and earlier. If set to True, then parsing of the characters ();<>|& is changed: any run of these characters (considered punctuation characters) is returned as a single token. If set to a non-empty string of characters, those characters will be used as the punctuation characters. Any characters in the wordchars attribute that appear in punctuation_chars will be removed from wordchars. See Improved Compatibility with Shells for more information. punctuation_chars can be set only upon shlex instance creation and can not be modified later.
                                                       Configparser vs Shlex Module
Configparser:
This module provides the ConfigParser class which implements a basic configuration language which provides a structure similar to what's found in Microsoft Windows INI files. You can use this to write Python programs which can be customized by end users easily.
Shlex:
Support for a creating Unix shell-like mini-languages which can be used as an alternate format for application configuration files.
                                                                       Using shlex class
import shlex
lexer = shlex.shlex(open("samples/sample.netrc""r"))
lexer.wordchars = lexer.wordchars + "._"

while 1:
    token = lexer.get_token()
    if not token:
        break
    print(repr(token))

Result:
'machine'
'secret.fbi'
'login'
'mulder'
'password'
'trustno1'
'machine'
'non.secret.fbi'
'login'
'scully'
'password'
'noway




More Articles of Rohit Kumar:

Name Views Likes
Python Copy ShallowCopy 323 4
Python PyGreSQL Classic PyGreSQL Interface Advanced Features 409 3
Python Nested Functions 398 3
Python PyGreSQL Classic PyGreSQL Interface DB Wrapper Class 475 3
Python Event Loop 391 4
Python PyGreSQL DB-API PyGreSQL Interface Functions 441 3
Python Shlex get_token() Method 681 4
Python Class Method Use 420 4
Python PyGreSQL Classic PyGreSQL Interface System Catalogs Examples 436 3
Python Multiple Vs Multi-level Inheritence 582 4
Python Shlex error_leader() Method 401 4
Python PyGreSQL Classic PyGreSQL Interface LargeObject 396 3
Python Bisect Insertion Functions 267 4
Python Multiple Inheritence 370 3
Python shlex class 500 4
Python Class Method 404 4
Python Shlex Quoted String 569 3
Python Shlex Methods 461 4
Python PyGreSQL Introduction 637 4
Python PasswordGenerator Project 769 4
Python Shlex Difference between Non-Posix and Posix Parsing 481 3
Python Shlex Functions 870 4
Python Shlex Split Example 520 3
Python PyGreSQL DB-API PyGreSQL Interface Cursor Object 447 3
Python Shlex Error-Handling 479 4
Python Copy deepcopy() 343 4
Python Shlex shlex.split() Vs re.split() 742 3
Python Server Program 748 4
Python Thumb Rule 636 4
Python Class Method Vs Static Method 535 4
Python PyGreSQL Classic PyGreSQL Interface Connection Object 476 4
Python PyGreSQL DB-API 2.0 Interface 465 4
Python PyGreSQL DB-API PyGreSQL Interface Connection Object 400 3
Python Bisect Functions 325 3
Python Loop Types 361 4
Python Bisect Introduction 472 4
Python Copy Introduction 310 4
Python PyGreSQL Classic PyGreSQL Interface Functions 457 3
Python PyGreSQL Classic PyGreSQL Interface SQL Functions Examples 415 3
Python Shlex Introduction 601 4
Python PyGreSQL Classic PyGreSQL Interface Basic Example 644 3
Python Shlex read_token() Method 505 3
Python Copy Shallowcopy Vs Deepcopy 295 4

Comments