PEP8 for long method's name [duplicate] - python

This question already has an answer here:
How to choose proper variable names for long names in python
(1 answer)
Closed 5 years ago.
What is the PEP8 correct way for long method's name?
I have a unit test with a self-describing method:
def success_if_buying_price_item_when_participating_and_progression_is_100_percent(self):
But unfortunately this (too long?) method reaches the 80 characters line limit.
Should I rename it and add a description in code or there is an other way?

Should I rename it and add a description in code or there is an other
way?
Yes, rename it and add a description. This code is dangerous, one could fall from its chair while another could have a heart attack. Please, rename that ASAP, you don't want to feel responsible.

Related

Python check if a variable appears in another variable [duplicate]

This question already has answers here:
Python efficient way to check if very large string contains a substring
(8 answers)
Closed 1 year ago.
I'm a beginner to Python. I'm using the request module to get the text from a website that contains blacklisted users for the login system of my program. I want to know how to check if a variable appears in another variable such as, "if variable appears in variable2: do something"
Can anyone help? Thanks.
You can check that using the in keyword -
if object1 in object2:
#do something
Share your code. It would give a better understanding of what you need to do. I think the below code will work.
import requests
x = requests.get('https://yourwebsite.com')
if variable in x.text:
#do something

Naming python variables [duplicate]

This question already has answers here:
What is the naming convention in Python for variable and function?
(15 answers)
Closed 2 years ago.
Any tips on how I should properly be naming python variables? Like any correct forms or for clean code.
Like for instance
myNum = 1
or
mynum = 1
or
my_num = 1
PEP 8 recommends my_num. #Merp's comment is correct though. Be clear and consistent with whatever convention you choose.
Well, traditionally developers follow the 'camelCase' variable naming proceedure in most languages, which is basically shown in your 1st code snippet. However, there are exceptions in the wide range of programming languages out there in terms of norms that manifest themselves over time. In Python, most developers tend to use the variable naming shown in your 3rd code snippet, separating each word with an underscore.

train['Gender'].fillna(train['Gender'].mode()[0], inplace=True) [duplicate]

This question already has answers here:
Uncomfortable output of mode() in pandas Dataframe
(4 answers)
Closed 4 years ago.
train['Gender'].fillna(train['Gender'].mode()[0], inplace=True)
I got this code in one of my basic data science course. I wanted to understand, what is the significance of "[0]" after mode() in this. I would really appreciate the answer.
Thanks!
Mode documentaion
The mode() return 2 value, first is mode value second is count. So train['Gender'].mode()[0] means get the mode value of train['Gender'].
The notation [0] means that the thing before it (mode() in this case) is a collection, a list, an array, ..., and you are taking the first element.
In case you need more information, you need to include the rest of the source code (preferably by editing your question), explaining the exact meaning of the mentioned objects.

Python Passing list by its name to a function [duplicate]

This question already has answers here:
How can I select a variable by (string) name?
(5 answers)
Closed 8 months ago.
names=['abcd','efgh']
nameoflist='names'
def(nameoflist=[]):
return nameoflist
I want to be able to return the entire list from the function
Assuming names is global as specified in the question, you can do this
names=['abcd','efgh']
nameoflist='names'
def return_names(nameoflist):
return globals()[nameoflist]
However, this is pretty ugly, and I'd probably try another way to do it. What do you need the name for? Is there any other way to get the information you're asking for?
This one way to do what you are asking. But it is not good programming.
names=['abcd','efgh']
def list_by_name(list_name):
return eval(list_name)
print(list_by_name('names'))
Also, argument list_name should be a string. It should not default to a list, which would make the function to fail if called without argument. It should not have a default value.

How would one create variables in a loop? [duplicate]

This question already has answers here:
Dynamic variable in Python [duplicate]
(2 answers)
Closed 8 years ago.
I would like to know how to create lots of variables by looping it. I know other people have asked this before but everyone who knows says you need a good reason for it and to just set it in a dictionary. My reason is that I need to assign up to 6156119580207157310796674288400203776 variables and there is no way I can do that by typing them out.
I need something like:
while counter < 1000:
try[counter] = counter
So that I could do this:
>>> try837
837
>>>try453
453
etc.
(this is an example not the exact code but any answer for this will solve my problem)
I would also like to know why people are opposed to answering this particular question. I don't want to tax my computer more than I already am by assigning this many variables so if it is an issue that could harm my computer or my code I would like to know.
You don't want to do this. Create a dictionary with a key for each suffix that you would use. Then use try[557] in place of the variable try557.
>>> try_ = dict((counter, counter) for counter in range(1000))
>>> print try_[557]
557
I'm using the standard technique of affixing an underscore to the otherwise reserved word "try".
(I'm ignoring the ludicrously large number of variables you claim to need.)

Categories

Resources