Skip to content

Instantly share code, notes, and snippets.

@etigui
etigui / test.py
Last active October 30, 2019 13:45
List comprehension vs. lambda + filter (json/SimpleNamespace)
# Ref: https://stackoverflow.com/questions/3013449/list-comprehension-vs-lambda-filter/3013686
# Ref json: https://catalog.data.gov/dataset/most-popular-baby-names-by-sex-and-mothers-ethnic-group-new-york-city-8c742
from types import SimpleNamespace
import json
import time
data_object = {}
with open('name.json', 'r') as f:
json_data = f.read()
data_object = json.loads(json_data, object_hook=lambda d: SimpleNamespace(**d))
@etigui
etigui / python_naming_conventions.md
Last active July 25, 2025 08:59
Python naming conventions

Python naming conventions

This document gives coding conventions example for the Python code. This style guide evolves over time as additional conventions are identified and past conventions are rendered obsolete by changes in the language itself.

1. General

  • Avoid using names that are too general or too wordy. Strike a good balance between the two.
    • Bad: data_structure, my_list, info_map, dictionary_for_the_purpose_of_storing_data_representing_word_definitions
    • Good: user_profile, menu_options, word_definitions
  • Never use the characters as single character variable names:
  • “l“ : lowercase letter el
@etigui
etigui / main_def.py
Last active August 13, 2019 09:09
Standard and object-oriented application main definition
# Standard
import sys
def main(args):
# My code here
pass
if __name__ == "__main__":
main(sys.argv)
@etigui
etigui / dist_azimuth.py
Created May 20, 2019 20:38
Get dist and azimuth between 2 coordinates (long, lat)
import pyproj
def main():
longFrom, latFrom = (6.090743, 46.225631)
longTo, latTo = (6.127212, 46.250516)
proj = pyproj.Geod(f'+proj=somerc +lat_0={latFrom} +lon_0={longFrom} +ellps=bessel +units=m +no_defs')
startAz, endAz, dist = proj.inv(longFrom, latFrom, longTo, latTo)
print(f'StartAz: {startAz}\nEndAz: {endAz}\nDist: {dist} [m]\n')
@etigui
etigui / lowercase_alphabet.py
Created May 14, 2019 09:41
Generating lowercase alphabet in Python (Google earth paddle)
import string
def main():
for s in string.ascii_uppercase:
print(f'http://maps.google.com/mapfiles/kml/paddle/{s}.png')
if __name__ == "__main__":
main()
@etigui
etigui / ignore_exceptions.py
Created March 17, 2019 10:36
Selectively ignore specific exceptions in Python 3.4+ (PyTricks)
# In Python 3.4+ you can use
# contextlib.suppress() to selectively
# ignore specific exceptions:
import contextlib
with contextlib.suppress(FileNotFoundError):
os.remove('somefile.tmp')
# This is equivalent to:
@etigui
etigui / else_in_loop.py
Created March 17, 2019 10:34
Loop "for" and "while"can have an "else" branch (PyTricks)
# Python's `for` and `while` loops
# support an `else` clause that executes
# only if the loops terminates without
# hitting a `break` statement.
def contains(haystack, needle):
"""
Throw a ValueError if `needle` not
in `haystack`.
"""
@etigui
etigui / check_elements_list_equal.py
Created March 17, 2019 10:32
Check if all elements in a list are equal in Python (PyTricks)
# Pythonic ways of checking if all
# items in a list are equal:
>>> lst = ['a', 'a', 'a']
>>> len(set(lst)) == 1
True
>>> all(x == lst[0] for x in lst)
True
@etigui
etigui / forced_keyword_only_parameters.py
Created March 17, 2019 10:30
Forced keyword-only parameters in Python 3.x (PyTricks)
# In Python 3 you can use a bare "*" asterisk
# in function parameter lists to force the
# caller to use keyword arguments for certain
# parameters:
>>> def f(a, b, *, c='x', d='y', e='z'):
... return 'Hello'
# To pass the value for c, d, and e you
# will need to explicitly pass it as
@etigui
etigui / multiple_args.py
Last active March 17, 2019 10:31
Passing multiple sets of keyword arguments to a function in Python 3.5+ (PyTricks)
# Python 3.5+ allows passing multiple sets
# of keyword arguments ("kwargs") to a
# function within a single call, using
# the "**" syntax:
>>> def process_data(a, b, c, d):
>>> print(a, b, c, d)
>>> x = {'a': 1, 'b': 2}
>>> y = {'c': 3, 'd': 4}