[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 338
  • Last Modified:

SyntaxError: can't assign to literal

I have a problem.

I use ConfigParser to parse ini files but I have a problem with my variable. It is a number ... and I get this error:

1080 = FHD('RFHD')
SyntaxError: can't assign to literal

Open in new window


1080 = FHD('RFHD')
720 = HD('RHD')
480 = SD('RSD')
#fin


config = ConfigParser.RawConfigParser()
config.read('config.ini')

print config.get('resolution', 'Write the resolution wish', 1080)
print config.get('resolution', 'Write the resolution wish', 720)
print config.get('resolution', 'Write the resolution wish', 480)

Open in new window

0
Gaaara
Asked:
Gaaara
  • 6
  • 5
  • 5
  • +2
5 Solutions
 
ozoCommented:
a1080 = FHD('RFHD')
a720 = HD('RHD')
a480 = SD('RSD')
#fin


config = ConfigParser.RawConfigParser()
config.read('config.ini')

print config.get('resolution', 'Write the resolution wish', a1080)
print config.get('resolution', 'Write the resolution wish', a720)
print config.get('resolution', 'Write the resolution wish', a480)
0
 
FarWestCommented:
don't know much about this scripting
but in general
this:
1080 = FHD('RFHD')
in most scripting should be this
FHD('RFHD') = 1080

because you assign values to variables
0
 
GaaaraAuthor Commented:
@ozo I had already made it and it had not worked

error

Traceback (most recent call last):
  File "/home/gaaara/adn/files/z«dxdfg.py", line 18, in <module>
    print config.get('resolution', 'Write the resolution wish', a1080)
TypeError: get() takes exactly 3 arguments (4 given)

Open in new window



@ FarWest  you right just this variable It is built in a way asked my function to import

full script
import ConfigParser
import sys

sys.path.append('files/')
from xrez import FHD
from xrez import HD
from xrez import SD
# variables
1080 = FHD('RFHD')
720 = HD('RHD')
480 = SD('RSD')
#fin


config = ConfigParser.RawConfigParser()
config.read('config.ini')

print config.get('resolution', 'Write the resolution wish', 1080)
print config.get('resolution', 'Write the resolution wish', 720)
print config.get('resolution', 'Write the resolution wish', 480)

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
gelonidaCommented:
can you try following and post the output:

sys.path.append('files/')
from xrez import FHD
print(FHD)
print(FHD('RFHD')

Open in new window


could you also post the contents of the .ini, tell us what exactly you try to do and post the exact error message of your script?

What seems to be strange is that you call
config.get() with three parameters.

what's your exact intention?
0
 
peprCommented:
Ozo just pointed out that any variable must start with a letter or underscore. It cannot start with a numeral. She just substituted your "numbers" by the variables that used a as the prefix. The bug with number of arguments is unrelated to the problem with variables.
0
 
GaaaraAuthor Commented:
i have 3 resolution possible in my script the use choose one with information stored in ini files and use the resolution with a variable


from xrez import FHD is a function in order script :)

my ini

[SETTINGS]
video_quality = 1080p

Open in new window

0
 
GaaaraAuthor Commented:
sys.path.append('files/')works
from xrez import FHD works
print(FHD) The good result does not give.
print(FHD('RFHD')    line error invalid syntax on this form it works print FHD('RFHD')

Him(It) resulted has to take(bring) out a link

exemple
http://www.foo.com:1945/kddd8dfesdkfjksdfu8sf9sfndfnsdf.mp4
0
 
gelonidaCommented:
@Gaaara

Could you please post the output of the command
print(FHD)

PLease change also the next command from
print(FHD('RFHD')
to
print(FHD('RFHD') )

the syntax error was there as there was one missing closing parenthesis
Please post also the output of this command.


I still don'tunderstand what you expect tohe program to do.

Could you perhaps show us which kind of output you want to see?

to get a value from configparser you should pass it a section and a value, so in your context I'd expect something like

resolution_from_config = config.get("SETTINGS", "resolution")

Open in new window


print(resolution_from_config) # should result in 1080p if your ini file was parsed correctly

You on the other hand pass three arguments to

config.get()
This does not make any sense.
Please look at the documentation of ConfigParser
https://docs.python.org/2/library/configparser.html

you will see in this document, that the expected parameters for get are
section and option


RawConfigParser.get(section, option)
    Get an option value for the named section.

In general it is a good idea to decompose 'complex' code lines into multiple lines for debugging.

for example:
instead of writing:
print config.get('resolution', 'Write the resolution wish', 480)

write
#
config_value = config.get('resolution', 'Write the resolution wish', 480)
print(config_value)

This makes it easier to see, that you pass the wrong number of arguments to config.get()
0
 
peprCommented:
Have a look at this example:
#!python2

import ConfigParser

# Create the config.ini for the purpose of demonstration.
fname = 'config.ini'

# Create the config object and fill it with the wanted information.
config = ConfigParser.ConfigParser()
config.add_section('SETTINGS')
config.set('SETTINGS', 'video_quality', '1080p')

# Write it to the file.
with open(fname, 'wb') as f:
    config.write(f)
    
# Show what is inside the file.
with open(fname) as f:
    print '---------------------------'
    for line in f:
        print line.rstrip()    
    print '---------------------------'

# Parse the config.ini into another configuration object.
cfg = ConfigParser.ConfigParser()
cfg.read(fname)

# Get the values from inside.
print cfg.get('SETTINGS', 'video_quality')

Open in new window

It prints on my screen:
c:\_Python\Gaaara\Q_28600304>a
---------------------------
[SETTINGS]
video_quality = 1080p

---------------------------
1080p

Open in new window

0
 
peprCommented:
The third argument of the .get is interpreted as boolean value, and it is related to substitution (templating, or how you want to call it). The default value--if wanted--must be passed as dictionary of default values -- passed as fourth argument. Try to modify the last lines:
# Get the values from inside.
print cfg.get('SETTINGS', 'video_quality')

default = {'some_option': 1000,
           'other_option': '2000xyz',
          }
print cfg.get('SETTINGS', 'some_option', True, default)
print cfg.get('SETTINGS', 'other_option', True, default)

Open in new window

0
 
gelonidaCommented:
@pepr: I'm lost:
 RawConfigParser.get() has only 2 parameters according to the python doc of my previous post
(At least for python 2.7)
However I did not try whether more args are supported so it could be the documentation is incomplete or that I looked at the wrong place.
0
 
peprCommented:
@gelonida: I did use ConfigParser, not the RawConfigParser. See https://docs.python.org/2/library/configparser.html#ConfigParser.ConfigParser.get
0
 
peprCommented:
@Gaaara: Do you want to get some information via xrez module and save it to ini file? Or should be the xrez values used as a default when getting the infor from the .ini?
0
 
GaaaraAuthor Commented:
It is necessary to use the values as default  or use the value That the user chose

sorry for time reposnse I do not have had time to look last message ( google trad  ) :)
0
 
GaaaraAuthor Commented:
it works !!!!!

global Resolution
config = ConfigParser.ConfigParser()
config.read('config.ini')
quality = config.get('SETTINGS', 'video_quality')
if quality == '1080p':
    Resolution = (quality_FHD('qualityFHD'))
elif quality == '720p':
    Resolution  = (quality_HD('qualityHD'))
elif quality == '480p':
    Resolution  = (quality_SD('qualitySD'))




print Resolution,'video_quality'

Open in new window

0
 
gelonidaCommented:
@Garaa you want to say, that NONE of the answers posted by the experts was helpful in solving your problem and that this is why you close this question and why you don't attribute any points.

I don't really need this points and if, then  it would probably be pepr who helped most with this question and who should get some points.

If I understand EE well, then  it's common practice in such situations to mark ones own question as answer and mark one or two posts of others as assisted answer,
0
 
gelonidaCommented:
@garaa:
Apologies: I noticed, that you closed the question but attributed points.
It must be me who's a little lost with how EE is working.
I thought the only way to attribute points was to not close a question.

Apologies again and Happy coding!
0
 
FarWestCommented:
@gelonida, I don't think @Garaa meant what you think,
the final accepted solution is his last post, so the EE knowledge base for someone has simile problem will be pointed to his post as a solution which is fair, the other post who awarded  points will be for assistance and contribution,
marking any other post as a solution will miss lead this question future viewers
regards
0
 
FarWestCommented:
@gelonida my reply was to this 2015-01-22 at 12:27:43,
but you are faster than me in writing :)
0
 
GaaaraAuthor Commented:
ty all for your help
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 6
  • 5
  • 5
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now