what is wrong with calling the help function? - python-3.x

I have problems calling the help function on the pd.read_csv()
pandas is already imported as pd
import pandas as pd
help(pd.read_csv())
and I get
Traceback (most recent call last):
File "<pyshell#21>", line 1, in <module>
help(pd.read_csv())
TypeError: parser_f() missing 1 required positional argument: 'filepath_or_buffer'
What is wrong with my help call?

in help(pd.read_csv()) you first called pd.read_csv() (cause of the parenthesis) so the interpreter was expecting an argument for, to execute it and return its result and pass it as argument to help.
The help function accepts functions as arguments so to show help execute help(pd.read_csv).
pd.read_csv is the function, pd.read_csv() is a call to that function.

Quite simply: don't call the object you want help on. Here:
help(pd.read_csv())
The parents after pd.read_csv are the function call operator. You don't want to call this function, you want to pass it as argument to help(), ie:
help(pd.read_csv)

Related

Revit Python Shell / Revit Python Wrapper - get Element name by id

I'm trying to get the name of an element by way the ID using Revit python wrapper in Revit python shell but I'm having issues. I am typically able to do it using c# but rpw is new to me.
I try:
doc.GetElement(2161305).name or doc.GetElement(2161305).Name
and I get this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: expected Reference, got int
I've looked a bit through the docs and watched some of the videos but haven't found anything that has covered this. I'm sure its easy, I'm just not not finding the answer.
Any help / direction is appreciated.
Got to answer my own question again.
>>> from rpw import db
>>> element = db.Element(SomeElement)
>>> element = db.Element.from_id(ElementId)
>>> element = db.Element.from_int(Integer) # this one worked for me
You need to cast the integer to an ElementId. The GetElement has three overloads. None of them takes an int, so you need to cast it to clarify which one is intended. Please read the GetElement documentation.

'DataFrame' object has no attribute 'get_value' in Pandas

Just learning python now, have very weak programming background.
I keep getting the error: 'DataFrame' object has no attribute 'get_value' using python 3.8.
The file is a random file I downloaded from the internet just to learn how to use dataframes and pandas. The object here is to pull a specific value out of the dataframe, so that I can manipulate it later.
import pandas as pd
pb_list = [] pb_list =
pd.read_csv(r"PB2010plus.csv") print(pb_list)
print(type(pb_list))
print(pb_list.get_value(1047, 'Winning Numbers'))
here's the error line
Traceback (most recent call last): File
"I:/Python/PycharmProjects/Learning Python 1/probabilityfunsheet.py",
line 8, in
print(pb_list.get_value(1047, 1)) File "C:\Users\greyb\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\generic.py",
line 5274, in getattr
return object.getattribute(self, name) AttributeError: 'DataFrame' object has no attribute 'get_value'
I am using pycharm, and did some searching, came across https://www.geeksforgeeks.org/python-pandas-dataframe-get_value/ which is where I got the idea as a potential solution for my 'problem'.
Starting it with an underscore worked for me
df._get_value(index,'name')
A good habit while reading data frames in Python is setting them as a variable:
import pandas as pd
pb_list = pd.read_csv("PB2010plus.csv")
Thus, to visualize them you won't need to print them, but you will just need to recall the variable pb_list.
# take a look to the dataframe
pb_list
# check the dataframe's type
type(pb_list)
# access to 1047 row index inside the Winning Numbers column
pb_list.get_value(1047, 'Winning Numbers')
However get_value has been deprecated and will be removed in a future release. Please use .at[] or .iat[] accessors instead.
Regarding your question. If you want to store the value that you are searching for in a variable to manipulate it in the future, here's the code:
# storing the desired value in target_value
target_value = pb_list.get_value(1047, 'Winning Numbers')
Please try using
df._get_value()
instead of
df.get_value()
The get_values method for a DataFrame was deprecated. Use values() instead. More info here
You don't need to put the result into a list pd_list = [] This code will give you an empty list and fill out this list with for loop in general. Try to remove that code and see what happens. Hope this helps.

NameError: name 'self' is not defined after using eval()

I am trying to save some copy-paste while defining my buttons and other things in my main class through eval(). I know eval() is supposed to be handled with care, but here I give the commands within my code. Here's the code that creates an error:
class MyApp(QMainWindow):
def __init__(self):
[...]
Schaltpunkte=["Aussen1","Innen1","Innen2","Innen","Alle"]
for Schaltpunkt in Schaltpunkte:
eval("self.ui.Button_"+Schaltpunkt+"Laden.clicked.connect(lambda: self.ladeZeitschaltung("+Schaltpunkt+"))")
The error I get once I clicked the button:
Traceback (most recent call last):
File "<string>", line 1, in <lambda>
NameError: name 'self' is not defined
I had this idea, because eval() works very well inside another function:
Programm = eval("self.ui.Box_"+Schaltpunkt+"Programm.value()")
Does someone have any advice? Or is it simply wrong to connect buttons with actions through such code? Thanks for your help!
Your lambda function is not aware of self.
Try to define a symbol table mentioning self, and give it to eval():
symbols = {"self": self}
eval("lambda x: self.foo(x)", symbols)

python modify builtin string functions

is it possible in Python to modify, or - at least - deny of execution builtin functions? I need, for educational purpose, to make sure that strings split is unavailable.
For example, I want, if call
'a,b,c'.split(',')
to throw exception or be returned inputed string.
I want to force someone to write own version of that function. Is it possible?
Thanks in advance!
Built-in types (str in your case) and methods cannot be monkey-patched, since they are implemented in C (for cpython implementation).
However, you can define a subclass and redefine the method:
>>> class Mystr(str):
... def split(self, *args):
... raise Exception("Split is not defined")
...
>>> Mystr("test").split(",")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in split
Exception: Split is not defined

How do I get a list from set in python 3

This seems to work in python 2.7, but not python 3. Is there an easy way to make a set a list in python 3 that I am missing? Thanks in advance.
mylist = [1,2,3,4,5]
list(set(mylist))
#Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
#TypeError: 'list' object is not callable
Sorry if this has been asked before, I did a quick search and didn't see an answer specific to python3.
list(set(...)) works fine. The error indicates the 3.x version of the code has a variable called list or set, shadowing the built-in function. Perhaps you renamed mylist to list? Rest assured, that mistake would provoke the exact same error message in Python 2.

Resources