Python3 check if ip address is global - python-3.x

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

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.

Revit Python Shell: How to get Type names of 'Pipe Types' Family with no instances in project?

End goal is to pass the ElementId of the PipeType I want (Plex Wire) to Pipe.Create, but I don't know how to select the correct PipeType ElementId in a project with no Pipe instances to inspect.
In a test project, I have used Transfer Project Standards to bring over the PipeType I want to use, and manually created a few Pipe instances to inspect...
>>> import Autodesk.Revit as R
>>> types=R.DB.FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_PipeCurves).WhereElementIsElementType().ToElements()
>>> elems=R.DB.FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_PipeCurves).WhereElementIsNotElementType().ToElements()
>>> for i in elems: print(i.Name)
...
Default
Default
Default
Plex Wire
>>> for i in types: print(i.Name)
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
AttributeError: Name
...but as I mentioned, I'd like to be able to use Pipe.Create from a project which contains the desired PipeTypes (from a Project Template), but has no pre-existing Pipe instances.
Thanks
I got Jeremy's 'transaction trick' to work (see below). Any critique on my code is appreciated, Thanks!
import Autodesk.Revit as R
pipeTypeNames={}
def GetPipeTypeNames():
types=R.DB.FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_PipeCurves).WhereElementIsElementType().ToElements()
pipingSystemTypes=R.DB.FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_PipingSystem).ToElements()
levels=R.DB.FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Levels).WhereElementIsNotElementType().ToElements()
pipeDoc=doc
pipeSystem=pipingSystemTypes[0].Id
pipeLevel=levels[0].Id
points=[]
transaction=R.DB.Transaction(doc,'Get Pipe Type Names')
transaction.Start()
for t in range(len(types)):
pipeType=types[t].Id
points.append((R.DB.XYZ(0,t,0),R.DB.XYZ(10,t,0)))
R.DB.Plumbing.Pipe.Create(pipeDoc,pipeSystem,pipeType,pipeLevel,points[t][0],points[t][1])
pipeElems=R.DB.FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_PipeCurves).WhereElementIsNotElementType().ToElements()
for p in pipeElems:
pipeTypeNames[p.Name]=p.PipeType
transaction.RollBack()
GetPipeTypeNames()
Use the ElementType FamilyName property introduced in Revit 2015.
Before that, the simplest option used to be to use the temporary transaction trick: open a transaction, insert a dummy instance, grab the desired name, and roll back the transaction.

pickle a zipfile.ZipFile with python >= 3.6

I came across some code that would not work anymore in python 3.6, but did good in all versions before. I found out the problem is actually a field containing a ZipFile somewhere in a class. Here is a short program which raises the error:
from pickle import dumps
import io
import zipfile
raw = b""
foo = zipfile.ZipFile(io.BytesIO(raw), mode="w")
dumps(foo)
I get this error:
Traceback (most recent call last):
File "bla.py", line 8, in <module>
dumps(foo)
TypeError: can't pickle _thread.RLock objects
So the test program can be even shorter:
from pickle import dumps
import threading
dumps(threading.RLock())
I diffed both the python 3.5 and 3.6 zipfile.py but can not spot any difference in respect to the _lock field in ZipFile, so it seems that there are changes in the threading module - but in threading.py there are also no obvious changes between the versions.
Why is it not pickable anymore? Do I need to do something before I can pickle a ZipFile?
Edit: ok after searching now for a while, I stumbled across this python bug tracker entry: https://bugs.python.org/msg284751
So that a ZipFile is pickable in python <3.6 is actually the bug...
I think I need to change a lot of code now...
Just to give an answer to this question: That ZipFile objects are pickable is actually a bug: https://bugs.python.org/msg284751 which has been fixed in py 3.6.

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.

"Default dictionary resource for language 'plnot found" error in jython while using a Morfologik library

I'm having quite a struggle trying to use Morfologik library in my project in jython. I need to use a PolishStemmer, so I imported morfologik-polish, morfologik-stemming and morfologik-fsa in jython console, using sys.path.append. However, each time I try to create a PolishStemmer object, the following situation occurs:
>>> p = PolishStemmer(PolishStemmer.DICTIONARY.COMBINED)
Traceback (most recent call last):
File "<console>", line 1, in <module>
RuntimeException: java.lang.RuntimeException: Default dictionary resource for language 'plnot found.
Now, according to the Morfologik API, this occurs only when the dictionary itself is not available. What additional .jars should I import to create and use PolishStemmer? (Note: I already tried adding morfologik-tools-standalone instead to the classpath, with no luck).

Resources