Repository navigation
Expand file tree
/
Copy pathclasses.py
More file actions
207 lines (163 loc) · 5.08 KB
/
Copy pathclasses.py
File metadata and controls
207 lines (163 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import sqlite3
import os
import logging
import re
import pandas as pd
folder = r"C:\Users\kelvi\Desktop"
db_name = "test_db.db"
db = rf"{folder}\{db_name}"
conn = sqlite3.connect(db) # if does not exist will be created
#cursor = conn.cursor()
try:
conn.execute("drop table if exists xyz;")
# commit the data
conn.commit()
# create a table
conn.execute("""
create table if not exists xyz (
id INTEGER PRIMARY KEY,
product TEXT,
item TEXT
)
""")
# commit the data
conn.commit()
# write some data
conn.executemany(
"insert or ignore into xyz (id, product, item) values (?, ?, ?)",
[
(1,"abv","abc"),
(2,"abc","abc")
],
)
# commit the data
conn.commit()
# query the data and write to csv
query_sql = "select * from xyz"
df = pd.read_sql_query(query_sql, conn)
df.to_csv( rf"{folder}\test_db.csv", index=False)
# process the rows
rows = conn.execute(query_sql).fetchall()
finally:
# close the connection
conn.close()
# function to run a query against specified db and return some data
def sql_fun(conn):
# table has already been created
# attach new databasetable to db
conn.execute(f"ATTACH DATABASE '{db}' AS db1")
# some new query
query = """
select
a.product as a_product,
b.product as b_product
from xyz a
left join db1.xyz b
on a.id=b.id
where a.product is not null
"""
# get rows and detach
# write as csv
df = pd.read_sql_query(query, conn)
df.to_csv( rf"{folder}\test_db_query.csv", index=False)
rows = conn.execute(query).fetchall()
conn.execute("DETACH DATABASE db1")
# some things to check
failures = [
{
"a_product": a_product,
"b_product": b_product,
}
for a_product,b_product in rows
]
failures.sort(
key=lambda failure: (
failure["a_product"] or "",
failure["b_product"] or "",
)
)
result = len(failures) == 0
message = (
f"{len(failures)} have products"
)
details = {
"failures": failures,
"field": "name",
"actual": len(failures),
"expected": 0,
}
return result, message, details
# test to check result is as expected
def test_sql():
t = sql_fun(conn=sqlite3.connect(db))
# access
assert t[0] is True # result
assert "products" in t[1] # message
assert "name" in t[2]["field"] # access details
# run test
try:
test_sql()
#finally
# close the connection
finally:
conn.close()
###############################################################################
import shapely
import shapely.wkt
from shapely import from_wkt
from shapely.ops import unary_union
from shapely.validation import explain_validity, make_valid
import pytest
import matplotlib.pyplot as plt
#input_wkt = "MULTIPOLYGON (((0 0, 10 0, 10 10, 0 10, 0 0)),((20 20, 21 20, 21 21, 20 21, 20 20), (2 2, 2 3, 3 3, 3 2, 2 2)))"
input_wkt = "POLYGON ((0 0, 0 2, 2 0, 2 2, 0 0))"
output_wkt = shapely.wkt.loads(input_wkt)
#assert not output_wkt.is_valid
#assert output_wkt.is_valid
invalud_shape = from_wkt(input_wkt)
typer = "poly"
if typer == "multi":
fixed_geom = unary_union([make_valid(part) for part in invalud_shape.geoms])
f_area = fixed_geom.area
for poly in invalud_shape.geoms:
fig, ax = plt.subplots()
x,y = poly.exterior.xy
ax.plot(x,y,color="#ff5722", linewidth=2)
ax.fill(x,y,color="#ff5722", alpha=0.3)
else:
fig, ax = plt.subplots()
x,y = invalud_shape.exterior.xy
ax.plot(x,y,color="#ff5722", linewidth=2)
ax.fill(x,y,color="#ff5722", alpha=0.3)
# or wktplot
#from wktplot import WKTPlot
#plot = WKTPlot()
#plot.add_shape(input_wkt)
###############################################################################
from http.server import BaseHTTPRequestHandler, HTTPServer
import json
import random
import time
class catdog:
def __init__(self,fur,name,age):
self.fur=fur
self.name=name
self.age=age
def type(self):
if self.fur == "straight":
return "cat"
elif self.fur == "other":
return "dog"
else:
return "unknown"
def noise(self):
animal = self.type()
if animal == "cat":
return "meow"
else:
return "woof"
animal1 = catdog(fur="other",name="daniel",age=5)
print(animal1.type())
print(animal1.noise())
pet1 = catdog("straight", "Whiskers", 3)
print(f"{pet1.name} says {pet1.noise()} and is a {pet1.type()}")