Skip to content

Commit

Permalink
Merge pull request #40 from Cheukting/dev
Browse files Browse the repository at this point in the history
Updating docstrings, minor fixes
  • Loading branch information
Cheukting authored Jan 22, 2020
2 parents efae2ce + 45d997c commit 7c12fa5
Showing 1 changed file with 43 additions and 39 deletions.
82 changes: 43 additions & 39 deletions woqlclient/woql.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ def get(self, arr1, arr2=None, target=None):
If WQOLQuery, arr2 will replace the target. If list, its column names of the source csv
arr2 : list or str
List, if arr1 is list and it will take the variable names for the input. Str, if arr1 is WQOLQuery, it will be the target
target : str, target of the source data, optional
Used only if arr1 and arr2 is list
target : str, optional
target of the source data, used only if arr1 and arr2 is list
Returns
-------
Expand Down Expand Up @@ -129,24 +129,24 @@ def check_vars_cols(obj):
clauses.append({'as': [v]})
return clauses

def typecast(self, va, type, vb):
def typecast(self, vara, type, varb):
"""Changes the type of va to type and saves the return in vb
Parameters
----------
va : str
vara : str
original variable
type : str
type to be changed
vb : str
varb : str
save the return variable
Returns
-------
WOQLQuery object
query object that can be chained and/or execute
"""
self.cursor['typecast'] = [va, type, vb];
self.cursor['typecast'] = [vara, type, varb];
return self

def insert(self, node, type, graph=None):
Expand Down Expand Up @@ -231,7 +231,7 @@ def file(self, json, opts=None):
Parameters
----------
json : dict
remote data source in a JSON format
file data source in a JSON format
opts : imput options, optional
Returns
Expand Down Expand Up @@ -373,10 +373,12 @@ def idgen(self, prefix, vari, type, mode=None):
----------
prefix : str
prefix for the id
vari : str
vari : str or list
variable to generate id for
type : str
the variable to hold the id
mode : str
idgen mode
Returns
-------
Expand Down Expand Up @@ -425,41 +427,41 @@ def unique(self, prefix, vari, type):
self.cursor['unique'].append(type)
return self

def re(self, p, s, m):
def re(self, pattern, test, matches):
"""Regular Expression Call
p is a regex pattern (.*) using normal regular expression syntax, the only unusual thing is that special characters have to be escaped twice, s is the string to be matched and m is a list of matches:
e.g. WOQL.re("(.).*", "hello", ["v:All", "v:Sub"])
Parameters
----------
p : str
pattern : str
regex pattern
s : str
test : str
string to be matched
m : str or list or dict
matches : str or list or dict
store list of matches
Returns
-------
WOQLQuery object
query object that can be chained and/or execute
"""
if type(p) == str:
if p[:2] != 'v:':
p = {"@value" : p, "@type": "xsd:string"}
if type(pattern) == str:
if pattern[:2] != 'v:':
pattern = {"@value" : pattern, "@type": "xsd:string"}

if type(s) == str:
if s[:2] != 'v:':
s = {"@value" : s, "@type": "xsd:string"}
if type(test) == str:
if test[:2] != 'v:':
test = {"@value" : test, "@type": "xsd:string"}

if type(m) == str:
m = [m]
if type(matches) == str:
matches = [matches]

if (type(m) != dict) or ('list' not in m):
m = {'list': m}
if (type(matches) != dict) or ('list' not in matches):
matches = {'list': matches}

self.cursor['re'] = [p, s, m]
self.cursor['re'] = [pattern, test, matches]
return self

def concat(self, list, v):
Expand Down Expand Up @@ -1119,7 +1121,7 @@ def divide(self, *args):
Parameters
----------
args : int or float
numbers to tbe divided
numbers to be divided
Returns
-------
Expand Down Expand Up @@ -1155,7 +1157,9 @@ def exp(self, a, b):
Parameters
----------
a : int or float
base number
b : int or float
power of
Returns
-------
Expand Down Expand Up @@ -1256,7 +1260,7 @@ def delete_quad(self, subject, predicate, object_or_literal, graph):
return self._chainable_update('delete_quad', subject)

def add_quad(self, subject, predicate, object_or_literal, graph):
"""Adds quads according to the the pattern [S,P,O]
"""Adds quads according to the pattern [S,P,O,G]
Parameters
----------
Expand All @@ -1282,12 +1286,12 @@ def add_quad(self, subject, predicate, object_or_literal, graph):

# Schema manipulation shorthand

def add_class(self, c=None, graph=None):
def add_class(self, classid=None, graph=None):
"""Generates a new Class with the given ClassID and writes it to the DB schema
Parameters
----------
c : str
classid : str
class to be added
graph : str, optional
target graph
Expand All @@ -1297,19 +1301,19 @@ class to be added
WOQLQuery object
query object that can be chained and/or execute
"""
if c:
if classid:
graph = self.clean_graph(graph) if graph else "db:schema"
self.adding_class = c
c = "scm:" + c if c.find(":") == -1 else c
self.add_quad(c, "rdf:type", "owl:Class", graph)
self.adding_class = classid
classid = "scm:" + classid if classid.find(":") == -1 else classid
self.add_quad(classid, "rdf:type", "owl:Class", graph)
return self

def delete_class(self, c=None, graph=None):
def delete_class(self, classid=None, graph=None):
"""Deletes the Class with the passed ID form the schema (and all references to it)
Parameters
----------
c : str
classid : str
class to be deleted
graph : str, optional
target graph
Expand All @@ -1319,12 +1323,12 @@ class to be deleted
WOQLQuery object
query object that can be chained and/or execute
"""
if c:
if classid:
graph = self._clean_graph(graph) if graph else "db:schema"
c = "scm:" + c if c.find(":") == -1 else c
classid = "scm:" + classid if classid.find(":") == -1 else classid

return self.woql_and(WOQLQuery().delete_quad(c, "v:All", "v:Al2", graph),
WOQLQuery().opt().delete_quad("v:Al3", "v:Al4", c, graph))
return self.woql_and(WOQLQuery().delete_quad(classid, "v:All", "v:Al2", graph),
WOQLQuery().opt().delete_quad("v:Al3", "v:Al4", classid, graph))
return self

def add_property(self, p=None, t=None, g=None):
Expand All @@ -1336,8 +1340,8 @@ def add_property(self, p=None, t=None, g=None):
property id to be added
t : str
type of the proerty
g : str
target graph ,optional
g : str, optional
target graph
Returns
-------
Expand Down

0 comments on commit 7c12fa5

Please sign in to comment.