16
16
from datetime import date , datetime , timedelta , timezone
17
17
18
18
import synapseclient
19
- from synapseclient .models import Column , ColumnType , CsvResultFormat , Row , Table
19
+ from synapseclient .models import Column , ColumnType , Table
20
20
21
21
PROJECT_ID = "syn52948289"
22
+ ROWS_TO_WRITE = 10
22
23
23
24
syn = synapseclient .Synapse (debug = True )
24
25
syn .login ()
@@ -36,15 +37,15 @@ def write_random_csv_with_data(path: str):
36
37
data = {}
37
38
for name , type in randomized_data_columns .items ():
38
39
if type == int :
39
- data [name ] = [random .randint (0 , 100 ) for _ in range (10 )]
40
+ data [name ] = [random .randint (0 , 100 ) for _ in range (ROWS_TO_WRITE + 1 )]
40
41
elif type == float :
41
- data [name ] = [random .uniform (0 , 100 ) for _ in range (10 )]
42
+ data [name ] = [random .uniform (0 , 100 ) for _ in range (ROWS_TO_WRITE + 1 )]
42
43
elif type == bool :
43
- data [name ] = [bool (random .getrandbits (1 )) for _ in range (10 )]
44
+ data [name ] = [bool (random .getrandbits (1 )) for _ in range (ROWS_TO_WRITE + 1 )]
44
45
elif type == str :
45
46
data [name ] = [
46
47
"" .join (random .choices (string .ascii_uppercase + string .digits , k = 5 ))
47
- for _ in range (10 )
48
+ for _ in range (ROWS_TO_WRITE + 1 )
48
49
]
49
50
50
51
with open (path , "w" , newline = "" , encoding = "utf-8" ) as csvfile :
@@ -54,7 +55,7 @@ def write_random_csv_with_data(path: str):
54
55
writer .writerow (data .keys ())
55
56
56
57
# Write data
57
- for i in range (10 ):
58
+ for i in range (ROWS_TO_WRITE + 1 ):
58
59
writer .writerow ([values [i ] for values in data .values ()])
59
60
60
61
@@ -86,13 +87,13 @@ def store_table():
86
87
87
88
# Creating a table ===============================================================
88
89
table = Table (
89
- name = "my_first_test_table " ,
90
+ name = "my_first_test_table_ksidubhgfkjsdgf " ,
90
91
columns = columns ,
91
92
parent_id = PROJECT_ID ,
92
93
annotations = annotations_for_my_table ,
93
94
)
94
95
95
- table = table .store_schema ()
96
+ table = table .store ()
96
97
97
98
print ("Table created:" )
98
99
print (table )
@@ -107,7 +108,7 @@ def store_table():
107
108
108
109
# Updating annotations on my table ===============================================
109
110
copy_of_table .annotations ["my_key_string" ] = ["new" , "values" , "here" ]
110
- stored_table = copy_of_table .store_schema ()
111
+ stored_table = copy_of_table .store ()
111
112
print ("Table updated:" )
112
113
print (stored_table )
113
114
@@ -116,31 +117,24 @@ def store_table():
116
117
path_to_csv = os .path .join (os .path .expanduser ("~/temp" ), f"{ name_of_csv } .csv" )
117
118
write_random_csv_with_data (path_to_csv )
118
119
119
- csv_path = copy_of_table .store_rows_from_csv ( csv_path = path_to_csv )
120
+ copy_of_table .store_rows ( values = path_to_csv )
120
121
121
- print ("Stored data to table from CSV:" )
122
- print (csv_path )
122
+ print ("Stored data to table from CSV" )
123
123
124
124
# Querying for data from a table =================================================
125
- destination_csv_location = os .path .expanduser ("~/temp/my_query_results" )
126
-
127
125
table_id_to_query = copy_of_table .id
128
- Table .query (
129
- query = f"SELECT * FROM { table_id_to_query } " ,
130
- result_format = CsvResultFormat (download_location = destination_csv_location ),
131
- )
126
+ dataframe_from_query = Table .query (query = f"SELECT * FROM { table_id_to_query } " )
132
127
133
- print (f"Created results at : { destination_csv_location } " )
128
+ print (f"Got results: { dataframe_from_query } " )
134
129
135
- # Deleting rows from a table =====================================================
136
- copy_of_table .delete_rows (rows = [ Row ( row_id = 1 )] )
130
+ # Deleting a row from the table =====================================================
131
+ copy_of_table .delete_rows (query = f"SELECT * from { table_id_to_query } LIMIT 1" )
137
132
138
133
# Deleting a table ===============================================================
139
134
table_to_delete = Table (
140
135
name = "my_test_table_I_want_to_delete" ,
141
- columns = columns ,
142
136
parent_id = PROJECT_ID ,
143
- ).store_schema ()
137
+ ).store ()
144
138
145
139
table_to_delete .delete ()
146
140
0 commit comments