1414from datetime import datetime , date , time
1515from enum import Enum
1616from typing import Tuple , get_args , get_origin , List , Optional , Union
17- import types_go
18- from types_py import *
1917from ctypes import (
2018 byref ,
2119 c_bool ,
3331)
3432import os
3533import platform
34+ import sys
35+ import types_go
36+ from types_py import *
3637
3738
38- def load_lib ():
39+ def load_lib () -> Optional [str ]:
40+ """
41+ Load the shared library based on the current platform and architecture.
42+ """
3943 system = platform .system ().lower ()
4044 arch = platform .architecture ()[0 ]
4145 machine = platform .machine ().lower ()
@@ -79,7 +83,7 @@ def load_lib():
7983 return f"libexcelize.{ arch_name } .{ system } { ext_map [system ]} "
8084
8185 print ("This platform or architecture is not supported." )
82- exit (1 )
86+ sys . exit (1 )
8387
8488
8589lib = CDLL (os .path .join (os .path .dirname (__file__ ), load_lib ()))
@@ -265,7 +269,7 @@ def get_c_field_type(struct, field_name):
265269 Returns:
266270 type: The type of the specified field if found, otherwise None.
267271 """
268- for field in struct . _fields_ :
272+ for field in getattr ( struct , " _fields_" , None ) :
269273 if field [0 ] == field_name :
270274 return field [1 ]
271275
@@ -319,7 +323,9 @@ def py_value_to_c(py_instance, ctypes_instance):
319323 if get_origin (arg_type ) is not list and arg_type is not bytes :
320324 # Pointer of the Go data type, for example: *excelize.Options or *string
321325 value = getattr (py_instance , py_field_name )
322- c_type = get_c_field_type (ctypes_instance , c_field_name )._type_
326+ c_type = getattr (
327+ get_c_field_type (ctypes_instance , c_field_name ), "_type_" , None
328+ )
323329 if value is not None :
324330 if any (is_py_primitive_type (arg ) for arg in py_field_args ):
325331 # Pointer of the Go basic data type, for example: *string
@@ -339,17 +345,21 @@ def py_value_to_c(py_instance, ctypes_instance):
339345 # The Go data type array, for example:
340346 # []*excelize.Options, []excelize.Options, []string, []*string
341347 if arg_type is bytes : # []byte
342- c_type = get_c_field_type (ctypes_instance , c_field_name )._type_
348+ c_type = getattr (
349+ get_c_field_type (ctypes_instance , c_field_name ), "_type_" , None
350+ )
343351 value = getattr (py_instance , py_field_name )
344- ctypes_instance . __setattr__ (
345- c_field_name , cast (value , POINTER (c_ubyte ))
352+ setattr (
353+ ctypes_instance , c_field_name , cast (value , POINTER (c_ubyte ))
346354 )
347- ctypes_instance . __setattr__ ( c_field_name + "Len" , c_int (len (value )))
355+ setattr ( ctypes_instance , c_field_name + "Len" , c_int (len (value )))
348356 continue
349357 py_field_type = get_args (arg_type )[0 ]
350358 if type (None ) not in get_args (py_field_type ):
351359 # The Go data type array, for example: []excelize.Options or []string
352- c_type = get_c_field_type (ctypes_instance , c_field_name )._type_
360+ c_type = getattr (
361+ get_c_field_type (ctypes_instance , c_field_name ), "_type_" , None
362+ )
353363 py_list = getattr (py_instance , py_field_name )
354364 if py_list :
355365 l = len (py_list )
@@ -358,7 +368,8 @@ def py_value_to_c(py_instance, ctypes_instance):
358368 # The Go basic data type array, for example: []string
359369 if str is py_field_type :
360370 c_array_type = POINTER (c_char ) * l
361- ctypes_instance .__setattr__ (
371+ setattr (
372+ ctypes_instance ,
362373 c_field_name ,
363374 c_array_type (
364375 * [
@@ -369,44 +380,44 @@ def py_value_to_c(py_instance, ctypes_instance):
369380 )
370381 else :
371382 for i in range (l ):
372- c_array .__setitem__ (
373- i , py_to_base_ctype (py_list [i ], c_type )
374- )
375- ctypes_instance .__setattr__ (c_field_name , c_array )
383+ c_array [i ] = py_to_base_ctype (py_list [i ], c_type )
384+ setattr (ctypes_instance , c_field_name , c_array )
376385 else :
377386 # The Go struct array, for example: []excelize.Options
378387 for i in range (l ):
379- c_array .__setitem__ (
380- i , py_value_to_c (py_list [i ], c_type ())
381- )
382- ctypes_instance .__setattr__ (c_field_name , c_array )
383- ctypes_instance .__setattr__ (c_field_name + "Len" , c_int (l ))
388+ c_array [i ] = py_value_to_c (py_list [i ], c_type ())
389+ setattr (ctypes_instance , c_field_name , c_array )
390+ setattr (ctypes_instance , c_field_name + "Len" , c_int (l ))
384391
385392 else :
386393 # Pointer array of the Go data type, for example: []*excelize.Options or []*string
387- c_type = get_c_field_type (
388- ctypes_instance , c_field_name
389- )._type_ ._type_
394+ c_type = getattr (
395+ getattr (
396+ get_c_field_type (ctypes_instance , c_field_name ),
397+ "_type_" ,
398+ None ,
399+ ),
400+ "_type_" ,
401+ None ,
402+ )
390403 py_list = getattr (py_instance , py_field_name )
391404 if py_list :
392405 l = len (py_list )
393406 c_array = (POINTER (c_type ) * l )()
394407 if is_py_primitive_type (get_args (py_field_type )[0 ]):
395408 # Pointer array of the Go basic data type, for example: []*string
396409 for i in range (l ):
397- c_array .__setitem__ (
398- i ,
399- pointer (py_to_base_ctype (py_list [i ], c_type )),
410+ c_array [i ] = pointer (
411+ py_to_base_ctype (py_list [i ], c_type )
400412 )
401413 else :
402414 # Pointer array of the Go struct, for example: []*excelize.Options
403415 for i in range (l ):
404- c_array .__setitem__ (
405- i ,
406- pointer (py_value_to_c (py_list [i ], c_type ())),
416+ c_array [i ] = pointer (
417+ py_value_to_c (py_list [i ], c_type ())
407418 )
408- ctypes_instance . __setattr__ ( c_field_name + "Len" , c_int (l ))
409- ctypes_instance . __setattr__ ( c_field_name , c_array )
419+ setattr ( ctypes_instance , c_field_name + "Len" , c_int (l ))
420+ setattr ( ctypes_instance , c_field_name , c_array )
410421 return ctypes_instance
411422
412423
@@ -439,6 +450,11 @@ def py_value_to_c_interface(py_value):
439450
440451
441452class StreamWriter :
453+ """
454+ StreamWriter is a streaming writer for writing large amounts of data to a
455+ worksheet.
456+ """
457+
442458 sw_index : int
443459
444460 def __init__ (self , sw_index : int ):
@@ -609,6 +625,10 @@ def flush(self) -> Optional[Exception]:
609625
610626
611627class File :
628+ """
629+ File is a representation of an workbook.
630+ """
631+
612632 file_index : int
613633
614634 def __init__ (self , file_index : int ):
@@ -1542,7 +1562,7 @@ def get_col_outline_level(
15421562 """
15431563 Get outline level of a single column by given worksheet name and column
15441564 name.
1545-
1565+
15461566 Args:
15471567 sheet (str): The worksheet name
15481568 col (str): The column name
@@ -1556,7 +1576,7 @@ def get_col_outline_level(
15561576
15571577 .. code-block:: python
15581578
1559- level, err = f.get_col_outline_level("Sheet1", "D")
1579+ level, err = f.get_col_outline_level("Sheet1", "D")
15601580 """
15611581 lib .GetColOutlineLevel .restype = types_go ._IntErrorResult
15621582 res = lib .GetColOutlineLevel (
0 commit comments