本文整理汇总了Python中shlex.shlex函数的典型用法代码示例。如果您正苦于以下问题:Python shlex函数的具体用法?Python shlex怎么用?Python shlex使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了shlex函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_new_data
def get_new_data(equity, last_date):
date_splitter = shlex.shlex(last_date, posix=True)
date_splitter.whitespace += '-'
date_splitter.whitespace_split = True
date = list(date_splitter)
last_date = str(date[2]) + '/' + str(date[1]) + '/' + str(date[0])
url = "http://www.bse-sofia.bg/graphics/phpfiles/MYgethistoDeA.php?MonCode=" + equity + "&MonPays=BE&Periode=1&De=" + last_date + "&A=" + datetime.date.today().strftime("%d/%m/%Y")
file_trades = urllib2.urlopen(url)
tradedays = file_trades.readline()
np_data = np.empty((0, 7))
for line in file_trades:
if not line.strip():
continue
# split main line
main_splitter = shlex.shlex(line.strip(), posix=True)
main_splitter.whitespace += ';'
main_splitter.whitespace_split = True
trade = list(main_splitter)
# if any trading for this day
if trade[1] != 'N':
# split date
date_splitter = shlex.shlex(trade[0], posix=True)
date_splitter.whitespace += '/'
date_splitter.whitespace_split = True
date = list(date_splitter)
# "Date" , "Open", "High", "Low", "Close", Volumes"
# 2012-12-21,10000,14046.26,1.423,1.4,1.4,1.423
np_data = np.append(np_data, [[date[2] + "-" + date[1] + "-" + date[0], float(trade[1]) / 100, float(trade[2]) / 100, float(trade[3]) / 100, float(trade[4]) / 100, -1, trade[5]]], axis=0)
return pand.DataFrame(data=np_data[:, 1:], index=np_data[:, 0], columns=("Open", "High", "Low", "Close", "AdjClose", "Volumes"), dtype=float)
示例2: split
def split(string, maxsplit=-1):
""" Split a string with shlex when possible, and add support for maxsplit. """
if maxsplit == -1:
try:
split_object = shlex.shlex(string, posix=True)
split_object.quotes = '"`'
split_object.whitespace_split = True
split_object.commenters = ""
return list(split_object)
except ValueError:
return string.split()
split_object = shlex.shlex(string, posix=True)
split_object.quotes = '"`'
split_object.whitespace_split = True
split_object.commenters = ""
maxsplit_object = []
splits = 0
while splits < maxsplit:
maxsplit_object.append(next(split_object))
splits += 1
maxsplit_object.append(split_object.instream.read())
return maxsplit_object
示例3: _expand_args
def _expand_args(command):
"""Parses command strings and returns a Popen-ready list."""
# Prepare arguments.
if isinstance(command, STR_TYPES):
if sys.version_info[0] == 2:
splitter = shlex.shlex(command.encode('utf-8'))
elif sys.version_info[0] == 3:
splitter = shlex.shlex(command)
else:
splitter = shlex.shlex(command.encode('utf-8'))
splitter.whitespace = '|'
splitter.whitespace_split = True
command = []
while True:
token = splitter.get_token()
if token:
command.append(token)
else:
break
command = list(map(shlex.split, command))
return command
示例4: parse
def parse(self, stream):
if isinstance(stream, unicode):
stream = stream.encode('utf-8')
# Convert stream to StringIO if necessary
stream = shlex.shlex(stream).instream
# Create lexer, no data by default
lex = shlex.shlex("", posix=True)
lex.wordchars += "!$%&()*+-./:x<=>[email protected]:"
lex.whitespace_split = False
lex.quotes = '"'
# Feed one line at the time
lines = []
prevline = []
for raw in stream:
lex.instream = shlex.StringIO(raw)
lex.state = ' '
newline = list(lex)
withContinuation = (newline[-1] == '\n') if newline else False
if withContinuation:
newline.pop()
if prevline:
prevline.extend(newline)
if not withContinuation:
prevline = []
continue
if withContinuation:
prevline = newline
lines.append(newline)
# Filter out empty lines
for line in lines:
if not line:
continue
self.parseLine(line)
return self
示例5: fetch_devices_for_host
def fetch_devices_for_host(host):
"""A successful search returns a list of theano devices' string values.
An unsuccessful search raises a KeyError.
The (decreasing) priority order is:
- PLATOON_DEVICES
- PLATOONRC files (if they exist) from right to left
- working directory's ./.platoonrc
- ~/.platoonrc
"""
# first try to have PLATOON_DEVICES
if PLATOON_DEVICES:
splitter = shlex.shlex(PLATOON_DEVICES, posix=True)
splitter.whitespace += ','
splitter.whitespace_split = True
return list(splitter)
# next try to find it in the config file
try:
try:
devices = platoon_cfg.get("devices", host)
except ConfigParser.InterpolationError:
devices = platoon_raw_cfg.get("devices", host)
except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
raise KeyError(host)
splitter = shlex.shlex(devices, posix=True)
splitter.whitespace += ','
splitter.whitespace_split = True
return list(splitter)
示例6: fetch_hosts
def fetch_hosts():
"""A successful search returns a list of host to participate in a multi-node
platoon. An unsuccessful search raises a KeyError.
The (decreasing) priority order is:
- PLATOON_HOSTS
- PLATOONRC files (if they exist) from right to left
- working directory's ./.platoonrc
- ~/.platoonrc
"""
# first try to have PLATOON_HOSTS
if PLATOON_HOSTS:
splitter = shlex.shlex(PLATOON_HOSTS, posix=True)
splitter.whitespace += ','
splitter.whitespace_split = True
return list(splitter)
# next try to find it in the config file
try:
try:
hosts = platoon_cfg.get("platoon", "hosts")
except ConfigParser.InterpolationError:
hosts = platoon_raw_cfg.get("platoon", "hosts")
except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
raise KeyError("hosts")
splitter = shlex.shlex(hosts, posix=True)
splitter.whitespace += ','
splitter.whitespace_split = True
return list(splitter)
示例7: testPunctuationWithPosix
def testPunctuationWithPosix(self):
"""Test that punctuation_chars and posix behave correctly together."""
# see Issue #29132
s = shlex.shlex('f >"abc"', posix=True, punctuation_chars=True)
self.assertEqual(list(s), ['f', '>', 'abc'])
s = shlex.shlex('f >\\"abc\\"', posix=True, punctuation_chars=True)
self.assertEqual(list(s), ['f', '>', '"abc"'])
示例8: split_ids
def split_ids(q):
'''split input query string into list of ids.
any of " \t\n\x0b\x0c\r|,+" as the separator,
but perserving a phrase if quoted
(either single or double quoted)
more detailed rules see:
http://docs.python.org/2/library/shlex.html#parsing-rules
e.g. split_ids('CDK2 CDK3') --> ['CDK2', 'CDK3']
split_ids('"CDK2 CDK3"\n CDk4') --> ['CDK2 CDK3', 'CDK4']
'''
# Python3 strings are already unicode, .encode
# now returns a bytearray, which cannot be searched with
# shlex. For now, do this terrible thing until we discuss
if sys.version_info.major == 3:
lex = shlex(q, posix=True)
else:
lex = shlex(q.encode('utf8'), posix=True)
lex.whitespace = ' \t\n\x0b\x0c\r|,+'
lex.whitespace_split = True
lex.commenters = ''
if sys.version_info.major == 3:
ids = [x.strip() for x in list(lex)]
else:
ids = [x.decode('utf8').strip() for x in list(lex)]
ids = [x for x in ids if x]
return ids
示例9: __init__
def __init__(self, instream=None):
if instream is not None:
self._lexer = shlex.shlex(instream, posix=True)
else:
self._lexer = shlex.shlex(posix=True)
self._lexer.whitespace += ','
self._lexer.wordchars += './()*-$'
self.eof = self._lexer.eof
示例10: parse_load
def parse_load(self, context, line, cursor):
if context.template:
self.warning(context, "load directives are not expected inside template definitions")
cursor, _ = self.parse_space(context, line, cursor)
cursor, path = self.parse_quoted(context, "'\"", "", line, cursor)
if not path:
raise Parse_Exception(context, "expected load path")
cursor, _ = self.parse_literal_req(context, "as", line, cursor)
cursor, alias = self.parse_identifier_req(context, line, cursor)
cursor, _ = self.parse_space(context, line, cursor)
params = []
cursor, params_text = self.parse_paren(context, "", line, cursor)
param_map = {
"path" : path,
"abs" : False,
"directive_token" : self.directive_token,
"placeholder" : self.placeholder
}
if params_text:
# split by comma, preserve quotes
comma_lex = shlex.shlex(params_text)
comma_lex.whitespace_split = True
comma_lex.whitespace = ","
comma_lex.commenters = ""
for param in comma_lex:
# split by eq, strip quotes
eq_lex = shlex.shlex(param, posix = True)
eq_lex.whitespace_split = True
eq_lex.whitespace += "="
eq_lex.commenters = ""
pair = list(eq_lex)
key = pair[0].strip()
value = True
if len(pair) > 1:
value = pair[1].strip()
param_map[key] = value
load = Template_Load()
load.alias = alias
load.path = param_map["path"]
load.abs = param_map["abs"]
load.directive_token = param_map["directive_token"]
load.placeholder = param_map["placeholder"]
self.trace(context,
"load directive: {} as {}, abs({}), directive_token({}), placeholder({})".format(
load.path, alias, load.abs, load.directive_token, load.placeholder))
context.module.template_loads.append(load)
示例11: parse
def parse(cls, string, context=None):
"""Parse a command string as usually is typed in console
to create a :class`Planner`.
:param `string`: the command line search to be parsed, using the
mole search syntax.
:param `context`: a dictionary to use as context for all actions.
Usually you want to use context to pass arguments to actions in
execution time.
"""
ret = cls()
shl = shlex.shlex(string)
shl.wordchars += ("\t "+RESERVED)
shl.whitespace = '|'
if context is None:
context = AttrDict({})
for cmd in shl:
cmd = " ".join(filter(lambda x:x not in KEYWORDS, cmd.split()))
if len(cmd) == 0:
continue
args = []
kwargs = {}
ishl = shlex.shlex(cmd)
ishl.wordchars += RESERVED
cmd = [ x.strip("\"").strip("'") for x in ishl ]
for arg in cmd[1:]:
if "=" in arg:
kwargs.update(dict([tuple(arg.split("="))]))
elif "," in arg:
args.append(arg.split(","))
else:
args.append([arg])
x = Action.from_type(cmd[0], *args, **kwargs)
x.context = AttrDict(context)
ret.add(x)
if isinstance(ret.queue[0], ActionInput) and x.context is not None:
obj = ret.queue[0].get_object()
if obj is not None:
if "plotter" in obj:
ret.add(obj.plotter)
else:
ret.add(Plotter.from_type("basic"))
if "parser" in obj:
ret.add(obj.parser)
else:
ret.add(Parser.from_type("basic"))
return ret
示例12: __init__
def __init__( self, f, prompts=['~# ', '~$ '], com='', whi='' ):
r"""
The constructor takes a filename or an open file or a string
as the shell session.
The constructor sets the :attr:`tokens` member attribute with
a shlex token stream initialised with the correct options for
parsing comments and whitespace.
The token commenters and whitespace are set to the empty string
and can be modified with the function arguments 'com' and
'whi'.
If the argument shlex_object is set to True then it'is not the
list of tokens but the shlex object itself so that you can
experiment with the :obj:`shlex` and it multiple attribute and
method.
>>> list(ShellSessionParser( "Yozza 1 2" ).tokens)
['Yozza', ' ', '1', ' ', '2']
>>> tokens = ShellSessionParser( "Yozza 1 2").tokens
>>> tokens.whitespace = ' '
>>> list(tokens)
['Yozza', '1', '2']
>>> list( ShellSessionParser("Yozza # a comment you dont want to see", whi=' ', com='#' ).tokens)
['Yozza']
"""
self.tokens = shlex( f if hasattr(f, "read") else StringIO( f ))
self.tokens.commenters = com
# deactivate shlex comments facility which won't work for us.
# The terminating linefeed means two things: end of comment an
# end of command. As shlex consume the terminating linefeed,
# there is no end of command left.
self.tokens.whitespace = whi
# deactivate shlex whitespace munging. characters cited in
# ``shlex.whitespace`` are not returned by get_token. If
# empty, whitespaces are returned as they are which is what we
# want: they definitely count in bash, and may count in
# output, so we just want to keep them as they are.
self.nested = 0
self.prompts = []
for p in prompts:
s=shlex(p)
s.commenters, s.whitespace = com, whi
self.prompts.append( list( s ) )
self.max_prompt_len = max([ len(p) for p in self.prompts ])
示例13: testEmptyStringHandling
def testEmptyStringHandling(self):
"""Test that parsing of empty strings is correctly handled."""
# see Issue #21999
expected = ['', ')', 'abc']
for punct in (False, True):
s = shlex.shlex("'')abc", posix=True, punctuation_chars=punct)
slist = list(s)
self.assertEqual(slist, expected)
expected = ["''", ')', 'abc']
s = shlex.shlex("'')abc", punctuation_chars=True)
self.assertEqual(list(s), expected)
示例14: testPunctuationWithWhitespaceSplit
def testPunctuationWithWhitespaceSplit(self):
"""Test that with whitespace_split, behaviour is as expected"""
s = shlex.shlex('a && b || c', punctuation_chars='&')
# whitespace_split is False, so splitting will be based on
# punctuation_chars
self.assertEqual(list(s), ['a', '&&', 'b', '|', '|', 'c'])
s = shlex.shlex('a && b || c', punctuation_chars='&')
s.whitespace_split = True
# whitespace_split is True, so splitting will be based on
# white space
self.assertEqual(list(s), ['a', '&&', 'b', '||', 'c'])
示例15: stream_video
def stream_video(url, open_chat):
global screen, play_process, stream_quality, stream_chat, DEVNULL
os.chdir(files_path)
command = stream_command + " " + url + " " + stream_quality
if stream_player != "":
command = command + " --player " + stream_player
lex = shlex.shlex(command)
lex.whitespace_split = True
args = list(lex)
try:
screen.clear()
screen.refresh()
set_status("starting stream")
if stream_chat and open_chat:
chat_command = "xdg-open " + url + "/chat"
chat_lex = shlex.shlex(chat_command)
chat_lex.whitespace_split = True
chat_args = list(chat_lex)
chat_process = subprocess.Popen(chat_args, stderr=DEVNULL)
chat_process.wait()
lex = shlex.shlex(command)
lex.whitespace_split = True
args = list(lex)
play_process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while True:
line = play_process.stdout.readline()
if not line:
break
set_status(line.decode("utf-8"))
logging.debug(line)
play_process.wait()
set_status("finished stream")
play_process = None
restore_state()
except:
set_status("playback failed")
play_process = None
restore_state()
return 1
return 0