How do I get a list from set in python 3 - python-3.x

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.

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.

PJSUA transfer call not working in python37 how ever it is working in python27

current_call.transfer("sip:1001#xx.xx.xx.xx") is working python2.7 How ever it is not working in python3.7
below is the error
Original exception was:
Traceback (most recent call last):
File "trycall.py", line 151, in <module>
current_call.transfer("sip:1001#xx.xx.xx.xx")
File "/usr/local/lib/python3.7/dist-packages/pjsua.py", line 1734, in transfer
Lib._create_msg_data(hdr_list))
SystemError: <built-in function call_xfer> returned NULL without setting an error
I am using this branch of PJSIP: Link
for PJSUA implementation with Python 3.6 and have encountered same problem with transfer.
Removing this check from function py_pjsua_call_xfer (pjproject/pjsip-apps/src/python/_pjsua.c) solved my problem:
if (!PyBytes_Check(pDstUri))
return NULL;
This check always returns NULL in all my tests. I was not able to solved this with Python. Removing code mentioned above solved the issue and for now it haven't created any new problems. I have tested this modification with Asterisk and 3 SIP endpoints and transfer was correctly processed.
(note: I am not a C/C++ programmer, so I cannot provide detail explenation why this code doesn't work. This approach is based on trial and error.)

'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.

Input wont accept anything except numbers

Input function is only accepting integers as an input otherwise I receive this error message when I run using REPL in python 3:
Entry:
b
Traceback (most recent call last):
File "Ex7.5.py", line 1, in <module>
a = input("Entry:\n")
File "<string>", line 1, in <module>
NameError: name 'b' is not defined
Need my code to accept both letters and numbers as input and cant understand why it's not taking b as a string and printing it?
Literally just trying to get this print input function to work currently to then use later in other functions.
If I run the same code with integers only it works no problem.
a = input("Entry:\n")
print(a)
print(type(a))
The answer I'm expecting is b.
This error is occurring because you're running the code in Python 2, not Python 3. input() in Python 2 will evaluate what you give it, in this case as a variable name, which doesn't exist; while input() in Python 3 will keep it as a string. For more details see What's the difference between raw_input() and input() in python3.x?
How to use the correct Python version is another question, but it looks like you're making some progress in the comments so far.

Python3 check if ip address is global

I'm trying to use python's "new" is_global method to determine, wether an ip address is allocated for public networks (https://docs.python.org/3/library/ipaddress.html#ipaddress.IPv4Address.is_global). However, this does not work:
>>> import ipaddress
>>> ip = ipaddress.IPv4Address('192.0.2.1')
>>> ip.is_private
True
>>> ip.is_reserved
False
>>> ip.is_global
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'IPv4Address' object has no attribute 'is_global'
>>>
As shown above, the other methods like is_private work fine. I'm using python 3.5.1.
Any insights?
I had a look at a bug report here and went to check the ipaddress.py module. While there exists an is_global attribute it is clearly not implemented and the bug report remains open and unresolved from Spring 2014 so I wouldn't hold my breath. If necessary you could get in touch with someone from the original bug report and get a status update.
UPDATE: According to user #galgalesh, at the time of writing this question, the is_global attribute was not implemented. That bug was resolved in Python 3.5 on 2016-06-11.
Attribute is_global is implemented as of Python 3.5+.

Resources