Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Python for ArcGIS Pro

You're reading from   Python for ArcGIS Pro Automate cartography and data analysis using ArcPy, ArcGIS API for Python, Notebooks, and pandas

Arrow left icon
Product type Paperback
Published in Apr 2022
Publisher Packt
ISBN-13 9781803241661
Length 586 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
 Toms Toms
Author Profile Icon Toms
Toms
 Parker Parker
Author Profile Icon Parker
Parker
Arrow right icon
View More author details
Toc

Table of Contents (4) Chapters Close

1. Python for ArcGIS Pro: Automate cartography and data analysis using ArcGIS Python modules, ArcGIS Pro Notebooks, Jupyter Notebooks, and pandas
2. Python: The Beginning FREE CHAPTER 3. Basics of ArcPy 4. ArcGIS API for Python

Basic style tips for writing scripts

To make clean, readable code, it is encouraged to follow these basic tips about how the code should be written and organized. The major rule enforced by Python is the required indentation, which is intended to make the code easier to read and write.

Read more about Python code style here: https://realpython.com/python-pep8/

Indentation

Python code has strict indentation rules that are enforced by all IDEs. These rules relate to functions and loops especially.

As a standard, 4 spaces are used after a function is declared or a loop is created. This is just a standard, as it could be only one space, but that indentation level becomes important when scripts get big and it helps to have 4 spaces for all indented lines so that they can be more easily read.

It is encouraged to not mix tabs and spaces when indenting.

Read more about indentation here: https://www.python.org/dev/peps/pep-0008/ - indentation

Add a comment at the top with script details

This is an optional but recommended way to start your scripts: write a comment at the top with your name, the date, and some quick explanation about what the script is supposed to do. This is especially nice when other people have to read your code.

Add lots of other comments throughout the script as well, to make sure you know what is happening throughout the script.

Follow with Import statements

It is encouraged but not required to put the import statements at or near the top of the script. Imports must happen before the module objects are called in the script, but the import statements can be placed anywhere. It is best to put them at the top so that people reading the script can understand what is being imported.

Define global variables

After the import statements, define the necessary variables that will be used in this script. Sometimes it is necessary to define variables later in the script but it is best to put major variables near the top.

Define functions

By placing function definitions below the global variables, it is easy to read and understand what the functions do when reading them. It is sometimes hard to find a function that is called in another part of the script if the function is not in a known location in the script.

Include print statements

The built-in function called print is used to send messages from the script to the command window while the script is running. Pass any valid data to the print statement and use it to track progress or to debug if there are issues.

>>> print("blueberry")
blueberry
>>> x = 0
>>> print(x)
0

Read more about print statements here: https://realpython.com/python-print/

Write the executable parts of the script

After importing modules and defining functions, the next part of the script is where the action takes place. The for loops are run, the functions are called, and the script is then done.

Make sure to add lots of comments to help yourself understand what is happening throughout the script, and print statements as well to help while the script is running.

If __name__ == ‘__main__’

Often at the end of scripts you will see this line, if __name__ == “__main__”. What it means is that the indented code below this line will be run if the script is executed directly, but if the code in the script is imported by another script, the code will not execute until called in the second script.

Read more about this here: https://www.geeksforgeeks.org/what-does-the-if-__name__-__main__-do/

You have been reading a chapter from
Python for ArcGIS Pro
Published in: Apr 2022
Publisher: Packt
ISBN-13: 9781803241661
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime
Visually different images