Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SyntaxError: can't assign to literal

Posted on 2015-01-20
20
Medium Priority
?
321 Views
Last Modified: 2015-01-26
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
Comment
Question by:Gaaara
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 5
  • +2
20 Comments
 
LVL 84

Assisted Solution

by:ozo
ozo earned 500 total points
ID: 40560282
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
 
LVL 12

Assisted Solution

by:FarWest
FarWest earned 500 total points
ID: 40560286
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
 

Author Comment

by:Gaaara
ID: 40560306
@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!

 
LVL 17

Assisted Solution

by:gelonida
gelonida earned 500 total points
ID: 40560778
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
 
LVL 29

Assisted Solution

by:pepr
pepr earned 500 total points
ID: 40560802
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
 

Author Comment

by:Gaaara
ID: 40560812
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
 

Author Comment

by:Gaaara
ID: 40560866
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
 
LVL 17

Expert Comment

by:gelonida
ID: 40560884
@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
 
LVL 29

Expert Comment

by:pepr
ID: 40560902
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
 
LVL 29

Expert Comment

by:pepr
ID: 40560913
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
 
LVL 17

Expert Comment

by:gelonida
ID: 40560987
@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
 
LVL 29

Expert Comment

by:pepr
ID: 40561753
@gelonida: I did use ConfigParser, not the RawConfigParser. See https://docs.python.org/2/library/configparser.html#ConfigParser.ConfigParser.get
0
 
LVL 29

Expert Comment

by:pepr
ID: 40561760
@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
 

Author Comment

by:Gaaara
ID: 40562295
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
 

Accepted Solution

by:
Gaaara earned 0 total points
ID: 40563602
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
 
LVL 17

Expert Comment

by:gelonida
ID: 40563849
@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
 
LVL 17

Expert Comment

by:gelonida
ID: 40563852
@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
 
LVL 12

Expert Comment

by:FarWest
ID: 40563854
@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
 
LVL 12

Expert Comment

by:FarWest
ID: 40563859
@gelonida my reply was to this 2015-01-22 at 12:27:43,
but you are faster than me in writing :)
0
 

Author Closing Comment

by:Gaaara
ID: 40570208
ty all for your help
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question