problem with optparse - OptionParse

A forum for general discussion of the Python programming language.

problem with optparse - OptionParse

Postby Ankush » Wed Oct 23, 2013 9:56 am

Hello everyone i am having a problem which i can't understand i am giving what i have done and what problem i am facing
here is my CODE
Code: Select all
#! /usr/bin/env python


import os

from dvbobjects.PSI.PAT import *
from optparse import OptionParser

#
# Shared values
#


parser = OptionParser()
parser.add_option( "--tsid",
                 help="input transportstream id", metavar="FILE")
(options, args) = parser.parse_args()

#############

avalpa_transport_stream_id = options.tsid # demo value, an official value should be demanded to dvb org
avalpa_original_transport_stream_id = 1#options.otsid # demo value, an official value should be demanded to dvb org
avalpa1_service_id = 1 # demo value
avalpa1_pmt_pid = 2031 #options.spmtid
output= './pat.ts'


#
# Program Association Table (ISO/IEC 13818-1 2.4.4.3)
#

pat = program_association_section(
   transport_stream_id = avalpa_transport_stream_id,
        program_loop = [
           program_loop_item(
           program_number = avalpa1_service_id,
          PID = avalpa1_pmt_pid,
           ), 
           program_loop_item(
           program_number = 0, # special program for the NIT
          PID = 16,
           ),
        ],
        version_number = 1, # you need to change the table number every time you edit, so the decoder will compare its version with the new one and update the table
        section_number = 0,
        last_section_number = 0,
        )

#
# PSI marshalling and encapsulation
#

out = open("./pat.sec", "wb")
out.write(pat.pack())
out.close
out = open("./pat.sec", "wb") # python   flush bug
out.close
os.system('/usr/local/bin/sec2ts 0 < ./pat.sec > '+ output)


#remove all sec files
os.system('rm *.sec')

and QUERY=
Code: Select all
./patconfig.py  --tsid 1

ERROR=
Code: Select all
Traceback (most recent call last):
  File "./patconfig.py", line 86, in <module>
    out.write(pat.pack())
  File "/usr/local/lib/python2.6/dist-packages/dvbobjects/MPEG/Section.py", line 94, in pack
    self.__sanity_check()
  File "/usr/local/lib/python2.6/dist-packages/dvbobjects/MPEG/Section.py", line 68, in __sanity_check
    assert 0 <= self.table_id_extension <= 0xffff
AssertionError

WHEN I DONT USE OPTPARSER SCRIPTS RUNS FINE
Last edited by Mekire on Wed Oct 23, 2013 10:07 am, edited 3 times in total.
Reason: First post lock. Code tags added. Please thoroughly read the "New Users read this" thread. Unneccessary capitilization also not appreciated.
Ankush
 
Posts: 4
Joined: Wed Oct 23, 2013 9:50 am

Re: problem with optparse - OptionParse

Postby Ankush » Thu Oct 24, 2013 5:16 am

Please provide me the solution!!!
Last edited by Mekire on Thu Oct 24, 2013 5:45 am, edited 1 time in total.
Reason: Please reconsider your tone when asking for free help.
Ankush
 
Posts: 4
Joined: Wed Oct 23, 2013 9:50 am

Re: problem with optparse - OptionParse

Postby Ankush » Thu Oct 24, 2013 6:01 am

Your help is needed kindly tell me what i am doing wrong
Ankush
 
Posts: 4
Joined: Wed Oct 23, 2013 9:50 am

Re: problem with optparse - OptionParse

Postby Mekire » Thu Oct 24, 2013 7:31 am

Add some print lines:
Code: Select all
from optparse import OptionParser

parser = OptionParser()
parser.add_option( "--tsid",
                 help="input transportstream id", metavar="FILE")
(options, args) = parser.parse_args()

print(options.tsid)
print(type(options.tsid))
Results:
Code: Select all
C:\Users\Owner\Desktop\opt_test>python parse.py --tsid 1
1
<type 'str'>

Note that options.tsid is a string; not a number.

You probably want:
Code: Select all
avalpa_transport_stream_id = int(options.tsid)
so that it is converted to a number.

This is just a guess.

-Mek

Edit:
Actually there is an even better way. You can state the desired type when you create the OptionParser.
Try this:
Code: Select all
parser.add_option( "--tsid", type="int",
                 help="input transportstream id", metavar="FILE")
User avatar
Mekire
 
Posts: 1138
Joined: Thu Feb 07, 2013 11:33 pm
Location: Asakusa, Japan

Re: problem with optparse - OptionParse

Postby Ankush » Fri Oct 25, 2013 5:03 am

Thank you very much sir..!! you solved my problem so easily . Thank you very much once again sir
Ankush
 
Posts: 4
Joined: Wed Oct 23, 2013 9:50 am


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 2 guests