Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why expected string become an tuple [duplicate]

I expected the variable output_format to be a string. But when I ran the script it gave me a tuple type and threw an exception.

If I run in Python interpreter, it gave me an expected string.

('--sout "#standard{access=file,vcodec=h264,dst=c0_s0_h264_640x480_30_vbr_500_99_40000000.mp4}"',)
'h264'
<type 'str'>
Traceback (most recent call last):
  File "streaming_verification/src/streaming_verification/scripts/streaming_verification.py", line 184, in run
    self.streaming.dump_file(export_fname, 5, codec_type)
  File "streaming_verification/src/streaming_verification/scripts/streaming.py", line 57, in dump_file
    cmd_str = " ".join(cmd)
TypeError: sequence item 3: expected string, tuple found

Script source code:

def dump_file(self,
              fname='',
              period=10,
              codec_type="h264"):

    if "h264" == codec_type:
        output_format = "--sout \"#standard{access=file,vcodec=h264,dst=%s.mp4}\"" % fname,
    elif "mjpeg" == codec_type:
        output_format =  "--sout \"#standard{access=file,vcodec=mjpg ,dst=%s.avi}\"" % fname,
    elif "mpeg" == codec_type :
        output_format =  "--sout \"#standard{access=file,vcodec=h264,dst=%s.mp4}\"" % fname,

    pp(output_format)

    cmd =[
    "vlc",
    "-I dummy",
    "--rtsp-tcp {0}".format(self.conn_cfg["rtsp_link"]),
    output_format,
    "--stop-time {0} vlc://quit ".format(period)
    ]
    cmd_str = " ".join(cmd)
    self.run(cmd_str)
like image 565
newBike Avatar asked Jan 08 '14 15:01

newBike


People also ask

What is the difference between tuple and string in Python?

Tuple they are immutable like strings and sequence like lists. They are used to store data just like list, just like string you cannot update or edit the tuple to change it you have to create a new one just like strings. Tuples can be created using parenthesis () and data is inserted using comas.

How do you duplicate a tuple?

Method #1 : Using * operator The multiplication operator can be used to construct the duplicates of a container. This also can be extended to tuples even though tuples are immutable.

Can tuples have different data types?

Tuples can be a collection of various data types, and unlike simpler data types, conventional methods of getting the type of each element of tuple is not possible.

Which mathematical operator is used to replicate a tuple?

Using the '*' operator. The repetition operator duplicates a tuple and links all of them together. Even though tuples are immutable, this can be extended to them.


1 Answers

Your output_format is always tuple, because you put a comma after each possible value:

output_format = "..." % fname,
# ---------------------------^

Remove those commas and your cmd_str will once again only contain strings.

Python tuples are formed by such commas; the parenthesis are only needed when not using them would lead to ambiguity:

>>> 1
1
>>> 1,
(1,)
>>> type(_)
<class 'tuple'>
like image 113
Martijn Pieters Avatar answered Nov 15 '22 21:11

Martijn Pieters