Skip to content

Instantly share code, notes, and snippets.

@xiaotianxt
Created March 21, 2022 10:55
Show Gist options
  • Save xiaotianxt/821157e271193572bca1ce1bea3f5929 to your computer and use it in GitHub Desktop.
Save xiaotianxt/821157e271193572bca1ce1bea3f5929 to your computer and use it in GitHub Desktop.
import os
from turtle import fd
import pandas as pd
import numpy as np
class Database:
FILENAME = 'db.csv'
def __init__(self, filename=FILENAME) -> None:
if not os.path.exists(filename):
with open(filename, 'w') as f:
f.write('index,lon,lat,tag')
self.load()
def __iter__(self):
for row in self.db.itertuples():
yield tuple(row)
def load(self, filename=FILENAME):
if not os.path.exists(filename):
raise FileNotFoundError(f"db file {filename} not found")
self.db = pd.read_csv(filename, dtype={
'lat': np.float64, 'lon': np.float64, 'tag': np.string_})
def save(self, filename=FILENAME):
self.db.to_csv(filename, index=False)
def insert(self, lat, lon, tag=np.nan):
self.db.loc[len(self.db), self.db.columns] = lat, lon, tag
self.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment