Python Forum
Uncanny line of code in Torchat source file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Uncanny line of code in Torchat source file
#1
I have been sifting through Torchat's sourcecode, which is available on github: https://github.com/prof7bit/TorChat

I don‘t understand one line of code within the config.py file, in the get(section, option) method, that is.

def get(section, option):
    if not config.has_section(section):
        config.add_section(section)
    if not config.has_option(section, option):
        value = config_defaults[section, option]
        set(section, option, value)
    value = config.get(section, option)
    if type(value) == str:
        try:
            value = value.decode("UTF-8")
            value = value.rstrip(" \"'").lstrip(" \"'")
        except:
            print "*** config file torchat.ini is not UTF-8 ***"
            print "*** this will most likely break things   ***"
    elif type(value) == int:
        value = str(value)
    elif type(value) == float:
        value = str(value)

    return value # this should now be a unicode string
value = config_defaults[section, option] assigns a valu to the value variable, and evidently, this value is taken from the config_defaults dictionary:

config_defaults = {
    ("tor", "tor_server") : "127.0.0.1",
    ("tor", "tor_server_socks_port") : 9050,
    ("tor", "tor_server_control_port") : 9051,
    ("tor_portable", "tor_server") : "127.0.0.1",
    ("tor_portable", "tor_server_socks_port") : 11109,
    ("tor_portable", "tor_server_control_port") : 11119,
   
     ...
    
   ("profile", "text") : "",
} 
It is not normally possible to get a value from a python dictionary like this: config_defaults[section, option], is it?

I haven't been able to figure out what the line of code in question does or how it is supposed to work. Can anyone gve me a hint?
Reply
#2
config_defaults is a dict that has tuples as keys.
And in this line value = config_defaults[section, option] section, option part is implicit tuple, so it's a valid code, effectively the same as value = config_defaults[(section, option)].

from the docs:
Quote:output tuples are always enclosed in parentheses, so that nested tuples are interpreted correctly; they may be input with or without surrounding parentheses, although often parentheses are necessary anyway (if the tuple is part of a larger expression).

>>> foo = 'spam', 'eggs'
>>> foo
('spam', 'eggs')
>>> type(foo)
<class 'tuple'>
>>> 
also, another example:


some_dict = {(1, 2):'one',
             (3, 4):'two'}
print(some_dict[1, 2])
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Now I understand. thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  delivery exe without source code py loky62 2 329 Apr-04-2024, 05:47 PM
Last Post: loky62
  Algorithm for extracting comments from Python source code Pavel1982 6 520 Feb-28-2024, 09:52 PM
Last Post: Pavel1982
  Unable to understand the meaning of the line of code. jahuja73 0 303 Jan-23-2024, 05:09 AM
Last Post: jahuja73
  Error on import: SyntaxError: source code string cannot contain null bytes kirkwilliams2049 7 6,682 Aug-03-2023, 06:00 PM
Last Post: Gribouillis
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 12,551 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  Getting last line of each line occurrence in a file tester_V 1 863 Jan-31-2023, 09:29 PM
Last Post: deanhystad
  Trying to loop through code to plot seaborn line plots across multiple subplots eyavuz21 0 1,663 Dec-05-2022, 10:46 AM
Last Post: eyavuz21
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,370 Sep-27-2022, 01:38 PM
Last Post: buran
  Print to a New Line when Appending File DaveG 0 1,217 Mar-30-2022, 04:14 AM
Last Post: DaveG
  Find and delete above a certain line in text file cubangt 12 3,464 Mar-18-2022, 07:49 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020