Package: derek-py
derekTools for converting data into schema.(Still very much pre-alpha!)Implemented in multiple languages.IndexCoverageSupported versionsDownloadsPython-JavaScript (node.js)Rust(coming soon!)----Nim(coming soon!)----InstallationWhat is Derek?Document data structuresExtract schemas from APIsReally lightweightExtensibleKISSDocumentationFeaturesSpecificationAPIInstallationPythonYou can install this from thepypiindex. It's available as thederek-pypackage.Simple example withpip(poetryis recommended):pipinstallderek-pyComplete set of supported installation methods:Package managerpypigitpippip install derek-pypip install git+https://github.com/benjaminwoods/derek@mainpoetrypoetry add derek-pypoetry add git+https://github.com/benjaminwoods/derek#mainJavascript (Node.js)You can install this from thenpmindex. It's available as thederek-tspackage.Simple example withyarn:yarnaddderek-tsComplete set of supported installation methods:Package managernpmgitnpmnpm i derek-tsnpm i git+https://github.com/benjaminwoods/derek#mainyarnyarn add derek-tsyarn add git+https://github.com/benjaminwoods/derek#mainWhat is Derek?Here's a quick guide showing what you can do withderek. These examples are
for a Python implementation.Derek documents data structures.Load some data into a tree of nodes:# Import the main classfromderekimportDerek# Suppose that you have some JSON-compatible dataobj=[{'some':[1.0,3,"4.5"],'data':[3.4,4.5]},{'some':[2,"4.0",1.5],'data':[1.4]}]# Feed this data into Derek.treeroot_node=Derek.tree(obj,name='MyDataStructure')You can use.example()to see a simple example item of data:>>>root_node.example()[{'some':[1.0],'data':[3.4]}]You can produce an OAS2/OAS3 JSON schema from this data, too:j=root_node.parse(format='oas3')importjsonprint(json.dumps(j,indent=2)){"MyDataStructure":{"type":"array","items":{"type":"object","additionalProperties":{"oneOf":[{"type":"array","items":{"oneOf":[{"type":"string"},{"type":"integer"},{"type":"number"}]}},{"type":"array","items":{"type":"number"}}]}},"example":[{"some":[1.0],"data":[3.4]}]}}Install and use theyamlpackage to
convert this structure to an OAS3-compliant data schema.importyamlprint(yaml.dump(j))MyDataStructure:example:-data:-3.4some:-1.0items:additionalProperties:oneOf:-items:type:numbertype:array-items:oneOf:-type:number-type:integer-type:stringtype:arraytype:objecttype:arrayDerek extracts schemas from APIs.Quickly extract schemas from APIs, by feeding the returned JSON into Derek.fromderekimportDerekfrompycoingeckoimportCoinGeckoAPIcg=CoinGeckoAPI()# Get all coins from CoinGeckoroot_node=Derek.tree(cg.get_coins_list(),name='GetCoins')Parse to get your schema:j=root_node.parse(format='oas3')importjsonprint(json.dumps(j,indent=2)){"GetCoins":{"type":"array","items":{"type":"object","additionalProperties":{"type":"string"}},"example":[{"id":"01coin","symbol":"zoc","name":"01coin"}]}}Derek is really lightweight.No required dependencies. Always.Derek is extensible.Use libraries likepywhatandyamlto quickly extendDerek:importjson,yamlfromderekimportDerek,ParserfrompywhatimportIdentifierclassPywhatDerek(Derek):@propertydefparser(self):returnPywhatParser()defget_oas3_yaml(self):returnyaml.dump(self.parse(format="oas3"))classPywhatParser(Parser):@classmethoddefoas2(cls,node):# Call the superclass parser for the current node:#   _sup = cls.__mro__[PywhatParser.__mro__.index(int):]#   j = _sup.oas2(cls, node)# All calls to the oas2 method in the superclass therefore re-route# back to this class method, automatically handling all recursive calls# here.j=super(PywhatParser,cls).oas2(node)# The rest of this function simply patches in results from a call# to the pywhat API.identifier=Identifier()ifall(map(lambdat:notisinstance(node.value,t),[list,dict])):result=identifier.identify(str(node.value))ifresult['Regexes']isnotNone:matches=[entryforentryinresult['Regexes']['text']]# Select the match as the longest stringmap_func=lambdad:(d['Matched'],d['Regex Pattern']['Name'])max_func=lambdatup:len(tup[0])_,match=max(map(map_func,matches),key=max_func)j={**j,'description':match}returnjAllowing for functionality like:root_node=PywhatDerek.tree({'data':['17VZNX1SN5NtKa8UQFxwQbFeFc3iqRYhem']},name='Addresses')root_node.get_oas3_yaml()returning:Addresses:additionalProperties:items:description:"Bitcoin(\u20BF)WalletAddress"type:stringtype:arrayexample:data:-17VZNX1SN5NtKa8UQFxwQbFeFc3iqRYhemtype:objectDerek is straightforward.Derek is designed for ease of use. If you're trying to use Derek functionality
in a workflow and it feels like it should be easier to get your desired result,
please make an issue.