Solved

SyntaxError: can't assign to literal

Posted on 2015-01-20
20
151 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
  • 6
  • 5
  • 5
  • +2
20 Comments
 
LVL 84

Assisted Solution

by:ozo
ozo earned 125 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 125 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
 
LVL 16

Assisted Solution

by:gelonida
gelonida earned 125 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 28

Assisted Solution

by:pepr
pepr earned 125 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 16

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 28

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 28

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 16

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 28

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 28

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 16

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 16

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 run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
no14 challenge 14 56
abstract class with all non abstract mentods 6 71
countX 22 70
Perl Write to Specific line in a file 15 51
This article will show, step by step, how to integrate R code into a R Sweave document
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

706 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now