User Tools

Site Tools


programming:python:snippets

Python - Snippets

Counting with a Dictionary

If you use an ordinary dict for counting, you have to check if the key already exists and if not, you have to initialize it. There is a better way. Use defaultdict from collections. You can give it a callable object for the initialization. If you give it int, the default value will be 0.

from collections import defaultdict
 
fruit_basket = ["apple", "banana", "apple", "plum", "apricot", "kiwi", "plum"]
 
fruits = defaultdict(int)
for fruit in fruit_basket:
    fruits[fruit] += 1
 
for fruit in fruits:
    print(f"{fruit}: {fruits[fruit]}")   
Output
apple: 2
banana: 1
plum: 2
apricot: 1
kiwi: 1

Read Lines from File

with open("fruit_basket.txt", "r") as f:
    fruit_basket_lines = f.read().splitlines()

If you use f.readlines() instead, all elements will contain a \n.

Cache expensive Functions

If you have a function which does an expensive task like a web request but you expect it to give you the same return value if the arguments are the same, then you can use the lru_cache function from functools and decorate your function with it. With maxsize (have to be a power of 2) you can define for how many arguments the return values are saved.

import urllib.request
from functools import lru_cache
 
@lru_cache(maxsize=32)
def get_robots_file(url="https://wiki.defect.ch/robots.txt"):
    with urllib.request.urlopen(url) as response:
        data = response.read()
    return data

If you call get_robots_file() multiple times, only one web request will be performed. The last 32 different urls (maxsize) will be cached that way.

Save/load Data Structures to/from a File

Save to File

import pickle
with open("mydata.pickle", "wb") as f:
    pickle.dump(mydata, f, pickle.HIGHEST_PROTOCOL)

Load from File

import pickle
with open("mydata.pickle", "rb") as f:
    mydata = pickle.load(f)

Save to a Compressed File

import gzip
import pickle
with gzip.open("mydata.pickle.gz", "wb") as f:
    pickle.dump(mydata, f, protocol=pickle.HIGHEST_PROTOCOL)

Load from Compressed File

import gzip
import pickle
with gzip.open("mydata.pickle.gz", "rb") as f:
    mydata = pickle.load(f)

Get Hostname

import socket
HOSTNAME = socket.gethostname()

Get Script Directory

import os.path
script_directory = os.path.dirname(os.path.realpath(__file__))
programming/python/snippets.txt · Last modified: 2024-06-21 17:10 by Manuel Frei