Python Formatting - Break up long line caused by indexing [duplicate] - python

This question already has answers here:
pep8 compliant deep dictionary access
(2 answers)
Closed 4 years ago.
I have a line accessing a very nested attribute in a dictionary that is now failing a PEP8 checker that was recently added to our release pipeline.
For example:
nested_attr = d['long_attr_name_1']['long_attr_name_2']['long_attr_name_3']['you_get_the_point']
What is the pythonic way to break up a line like this so it does not exceed the style guide line limit check?

nested_attr = (d['long_attr_name_1']['long_attr_name_2']
['long_attr_name_3']['you_get_the_point'])
Wrapping in brackets if a PEP8 saviour!

Related

How to call dynamicly string operations in Python? [duplicate]

This question already has answers here:
How do I execute a string containing Python code in Python?
(14 answers)
Closed last month.
//func_to_exec parameter is coming from database dynamically.
func_to_exec='split("\|")[0].split(",")[1]'
pl='mancity,manunited,arsenal|2|3|4|5'
is there anyway to call
pl.func_to_exec
I saw exec and eval functions are only for integers. I cant find any solution for strings.
Thx for suggestions.
You can use the exec function for that:
pl = 'mancity,manunited,arsenal|2|3|4|5'
func_to_exec = 'split("\|")[0].split(",")[1]'
exec(f'result = pl.{func_to_exec}')
print(result) # Output: 'manunited'

How to break a long python line (assignment) with very long variable names [duplicate]

This question already has answers here:
How can I do a line break (line continuation) in Python?
(10 answers)
Closed 10 months ago.
This is not my code, but I'm asked to fix some flake8 lint errors.
There's a line in the code that looks like this
some_very_longggg_variable_name_on_the_lhs = SomeLongClassNameIsHere.func_name(input_dt=some_default_input_dt_that_has_a_really_long_name_here)
I'm unable to change the names of the variables. The number of columns is 144 and I need to get it to 120.
It doesn't allow me to break it at the equal sign like the following and I also tried putting the argument of func_name on the next line, but flake8 requires aligning the new line with the ( on the previous line so that doesn't help. I'm not sure what else to do.
some_very_longggg_variable_name_on_the_lhs
= SomeLongClassNameIsHere.func_name(input_dt=some_default_input_dt_that_has_a_really_long_name_here)
Python will not break a statement inside parentheses of any kind, so
a = (
b
)
is the simplest, most legible and most maintainable way to do this.

Python sort function explanation [duplicate]

This question already has answers here:
What algorithm does Python's built-in sort() method use?
(2 answers)
What is `lambda` in Python code? How does it work with `key` arguments to `sorted`, `sum` etc.?
(4 answers)
Understanding the map function
(6 answers)
Closed 1 year ago.
I found the following line of code which I'm unable to google as it has a custom function. Can someone please explain this?
list.sort(key=lambda v: map(int, v.split('.')))
I know this sorts a list but I want to understand which sort is it and how it works.

How to call various strings without using %s? [duplicate]

This question already has answers here:
Type Error: Format Requires Mapping
(2 answers)
Closed 3 years ago.
text1="Python"
text2="with me"
print("Study %(language)s" %{'language':text1})
This works. But I am wondering whether it is using dictionary to call string?
print("Study %(language)s %(with whom)" %({'language':text1},{'with whom':text2}))
But it doesn't work. How can I fix it?
The error says 'format requires a mapping'
It would have worked, if you noticed that:
you've forgotten to put an s after %(with whom) --> %(with whom)s,
and instead of this %({'language':text1},{'with whom':text2}) --> %{'language':text1,'with whom':text2}
so the line would be like this:
print("Study %(language)s %(with whom)s" %{'language':text1,'with whom':text2})

Ruby's "next" implementation in Python [duplicate]

This question already has answers here:
How can I get the next string, in alphanumeric ordering, in Python?
(4 answers)
Closed 6 years ago.
If Python has an implementation of Ruby's next method? I mean something what works exactly the same as in Ruby, so if I type e.g. "z".next it will return "aa" (instead of just next sign in ascii table), "az".next will return "ba" and so on.
I don't believe there is a built-in method for this in Python. A similar question was asked on How can I get the next string, in alphanumeric ordering, in Python? and the accepted answer gives a solution.

Categories