Breaking time

1403
11
09-12-2018 12:01 AM
HåkonDreyer
Esri Contributor

Hi, it seems importing the GIS breaks a former from time import time import as demonstrated in the following example.

          Code sample

Switching the imports works, but it's not intuitive, and neither very PEP 8.

It seems time.sleep() is the only function from the time module that are used in the API, and it is only used twice, in Item.export() and Item._check_publish_status().
Still, there are three instances of import time scattered around in the __init__.py file. Wouldn't it be better to narrow the import to a from time import sleep in the imports at top of the file?
Tags (2)
0 Kudos
11 Replies
DanPatterson_Retired
MVP Emeritus
0 Kudos
HåkonDreyer
Esri Contributor

Guess I could, but the take from What is the preferred way to report found bugs in the Python API? and https://community.esri.com/thread/196929-geonet-or-github-for-technical-questions-about-api was that GeoNet was the preferred channel for this kind of reports.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Yes, but it is nice to see it on GitHub as well for those that don't travel geonet

0 Kudos
RohitSingh2
Esri Contributor

I am unable to reproduce it with v1.5. What version of the Python API are you using?

0 Kudos
HåkonDreyer
Esri Contributor

Strange, I'm on 1.5. 

0 Kudos
HåkonDreyer
Esri Contributor

But then, when testing in an environment not cloned from Pro it works…

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I think this is more of an ArcPy issue than an ArcGIS API for Python issue, but it becomes an issue for the latter when the former is imported.  If you import ArcPy first (not suggesting it as a workflow/workaround, just for testing), then importing from arcgis.gis doesn't clobber your time import.

(arcgis-py3) C:\Users\user\AppData\Local\ESRI\conda\envs\arcgis-py3>python
Python 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from time import time
>>> type(time)
<class 'builtin_function_or_method'>
>>> import arcpy
>>> type(time)
<class 'module'>
>>> from time import time
>>> type(time)
<class 'builtin_function_or_method'>
>>> from arcgis.gis import GIS
>>> type(time)
<class 'builtin_function_or_method'>
>>>

Unfortunately, importing from datetime and time can be tricky since the modules contain classes with the exact same names.  For that reason, I just import the time module since time() doesn't save much over time.time().  For datetime, I usually do from datetime import datetime as dt.

It would be great if we had a time.Time class instead of a time.time class, and a datetime.Datetime class instead of a datetime.datetime, but I don't know the full history of why those two modules ended up the way they did.

0 Kudos
HåkonDreyer
Esri Contributor

Good point, guess it explains the difference between cloned Pro-environments and environments built in Conda.

And yes, switching the imports works, but it is neither intuitive nor good style as mentioned in the original post.

0 Kudos
DanPatterson_Retired
MVP Emeritus

maybe arcpy used have used 'thyme' instead

0 Kudos