[Application-profiles-ig] Python implementation(s)

Nishad Thalhath nishad at thalhath.org
Sat Dec 5 15:23:42 GMT 2020


Hi Tom, A quick response on CSV reader for empty values : The default behavior always depends on the implementation/library/subclasses. If we assume that, we use the built-in CSV module to parse, in 'csv.DictReader', the default is 'None', and we can override it with 'restval'. 'csv.reader' doesn't provide any direct alternatives to override this behaviour. For DCTAP, DictReader is much suitable since the model doesn't have any duplicate header. As you have stated, Pythonically, it is all about truthiness. Nishad From: Application-profiles-ig <application-profiles-ig-bounces at lists.www.voudr.com> Date: Saturday, December 5, 2020 23:26 To: Phil Barker <phil.barker at pjjk.co.uk> Cc:application-profiles-ig at lists.www.voudr.com<application-profiles-ig at lists.www.voudr.com>, Harold Solbrig <solbrig at jhu.edu> Subject: Re: [Application-profiles-ig] Python implementation(s) Hi Phil, Cc: Harold, attn: Nishad, On 2020-12-05 12:48, Phil Barker wrote: >I found the csvschema branch of your csv2shex.py yesterday, and was>working with it in the afternoon.That's good to know, thanks. I worked on that branch yesterday a bit with EricP and with Harold, and worked with it again a bit this morning, so make sure you pull the latest. >As a python structure I like it, but>I'm still swithering between this approach of class-properties and the>alternative of just using dicts and lists.I was wondering the same! In a previous iteration, I constructed lists of CSVRow and CSVShape objects but ended up outputting, at the end, a list of dictionaries. I asked Harold Solbrig (in Cc:) your very question. As I understand it, one rationale is readability. The class names are useful as handles that show the reader what the component parts of some nested construct of lists and dicts, for example, are about. I understand there are also some benefits for users of IDEs such as PyCharm involving, I think, type hints. >The main advantage I see in the latter is support from>standard python libraries. For example getting a>printout with pprint, converting to JSON with>json.dumps().It is easy to turn an instance of CSVShape, for example, into a dictionary -- see line [1]: csvshape_dict = asdict(csvshape_obj) which converts CSVShape( shapeID=':a', ... tc_list=[CSVTripleConstraint(propertyID='dct:creator', into { 'shapeID': ':a', ... 'tc_list': [{'propertyID': 'dct:creator'... [1]https://github.com/tombaker/csv2shex/blob/csvschema/csv2shex/inspect.py#L16>It's possibly more flexible too, the value for>TripleConstraints["valueConstraint"] could be a string>or a list.It is not a problem to use Union or Optional types, for example, to annotate the field of a dataclass (see [2]) - or to write methods that accept alternative types of input. [2]https://stackoverflow.com/questions/55472320/can-i-use-the-union-and-optional-types-from-the-typing-module-when-creating-a-daA question for you (or Nishad): do you happen to know what difference it makes -- in the case of dataclasses designed to be populated from CSV files (ie, with string values) -- whether the default value should be an empty string, None or even, in some cases, False? @dataclass class CSVTripleConstraint: ... mandatory: str = "" mandatory: str = None mandatory: str = False bool() evaluates all three to False, but perhaps one way is more pythonic, or perhaps there are hidden gotchas with one or the other? Tom -- Tom Baker <tom at tombaker.org> -------------- 下一个部分  -------------- 一个HTMLattachment was scrubbed... URL: <https://lists.www.voudr.com/pipermail/application-profiles-ig/attachments/20201205/2d9028e7/attachment.htm>


More information about the Application-profiles-ig mailing list