Classes have now names that begin with uppercase

This is more Pythonic and makes the code more readable.

Backwards compatibility is provided by giving an alias with the
previous name of the class.

It will not be broken until the next major release.
master
Salvo 'LtWorf' Tomaselli 2015-07-14 15:39:35 +07:00
parent af02b5a59b
commit be7e6fe12d
5 changed files with 31 additions and 26 deletions

@ -60,7 +60,7 @@ def check_latest_version():
return s.decode().strip() return s.decode().strip()
class user_interface (object): class UserInterface (object):
'''It is used to provide services to the user interfaces, in order to '''It is used to provide services to the user interfaces, in order to
reduce the amount of duplicated code present in different user interfaces. reduce the amount of duplicated code present in different user interfaces.

@ -99,7 +99,7 @@ class CallableString(str):
return eval(self, context) return eval(self, context)
class node (object): class Node (object):
'''This class is a node of a relational expression. Leaves are relations '''This class is a node of a relational expression. Leaves are relations
and internal nodes are operations. and internal nodes are operations.
@ -416,7 +416,5 @@ if __name__ == "__main__":
e = input("Expression: ") e = input("Expression: ")
print (parse(e)) print (parse(e))
# b=u"σ age>1 and skill=='C' (peopleᐅᐊskills)" #Backwards compatibility
# print b[0] node = Node
# parse(b)
pass

@ -26,7 +26,7 @@ from collections import deque
from relational.rtypes import * from relational.rtypes import *
class relation (object): class Relation (object):
''' '''
This object defines a relation (as a group of consistent tuples) and operations. This object defines a relation (as a group of consistent tuples) and operations.
@ -57,11 +57,11 @@ class relation (object):
if len(filename) == 0: # Empty relation if len(filename) == 0: # Empty relation
self.content = set() self.content = set()
self.header = header([]) self.header = Header([])
return return
with open(filename) as fp: with open(filename) as fp:
reader = csv.reader(fp) # Creating a csv reader reader = csv.reader(fp) # Creating a csv reader
self.header = header(next(reader)) # read 1st line self.header = Header(next(reader)) # read 1st line
self.content = set() self.content = set()
attributes = len(self.header) attributes = len(self.header)
@ -121,7 +121,7 @@ class relation (object):
Selection, expr must be a valid Python expression; can contain field names. Selection, expr must be a valid Python expression; can contain field names.
''' '''
newt = relation() newt = relation()
newt.header = header(self.header) newt.header = Header(self.header)
for i in self.content: for i in self.content:
# Fills the attributes dictionary with the values of the tuple # Fills the attributes dictionary with the values of the tuple
attributes = {attr: i[j].autocast() attributes = {attr: i[j].autocast()
@ -148,7 +148,7 @@ class relation (object):
'Unable to perform product on relations with colliding attributes' 'Unable to perform product on relations with colliding attributes'
) )
newt = relation() newt = relation()
newt.header = header(self.header + other.header) newt.header = Header(self.header + other.header)
for i in self.content: for i in self.content:
for j in other.content: for j in other.content:
@ -178,7 +178,7 @@ class relation (object):
newt = relation() newt = relation()
# Create the header # Create the header
h = (self.header[i] for i in ids) h = (self.header[i] for i in ids)
newt.header = header(h) newt.header = Header(h)
# Create the body # Create the body
for i in self.content: for i in self.content:
@ -213,7 +213,7 @@ class relation (object):
''' '''
other = self._rearrange(other) # Rearranges attributes' order other = self._rearrange(other) # Rearranges attributes' order
newt = relation() newt = relation()
newt.header = header(self.header) newt.header = Header(self.header)
newt.content = self.content.intersection(other.content) newt.content = self.content.intersection(other.content)
return newt return newt
@ -224,7 +224,7 @@ class relation (object):
''' '''
other = self._rearrange(other) # Rearranges attributes' order other = self._rearrange(other) # Rearranges attributes' order
newt = relation() newt = relation()
newt.header = header(self.header) newt.header = Header(self.header)
newt.content = self.content.difference(other.content) newt.content = self.content.difference(other.content)
return newt return newt
@ -261,7 +261,7 @@ class relation (object):
''' '''
other = self._rearrange(other) # Rearranges attributes' order other = self._rearrange(other) # Rearranges attributes' order
newt = relation() newt = relation()
newt.header = header(self.header) newt.header = Header(self.header)
newt.content = self.content.union(other.content) newt.content = self.content.union(other.content)
return newt return newt
@ -298,7 +298,7 @@ class relation (object):
# Creating the header with all the fields, done like that because order is # Creating the header with all the fields, done like that because order is
# needed # needed
h = (i for i in other.header if i not in shared) h = (i for i in other.header if i not in shared)
newt.header = header(chain(self.header, h)) newt.header = Header(chain(self.header, h))
# Shared ids of self # Shared ids of self
sid = self.header.getAttributesId(shared) sid = self.header.getAttributesId(shared)
@ -342,7 +342,7 @@ class relation (object):
# Creating the header with all the fields, done like that because order is # Creating the header with all the fields, done like that because order is
# needed # needed
h = (i for i in other.header if i not in shared) h = (i for i in other.header if i not in shared)
newt.header = header(chain(self.header, h)) newt.header = Header(chain(self.header, h))
# Shared ids of self # Shared ids of self
sid = self.header.getAttributesId(shared) sid = self.header.getAttributesId(shared)
@ -484,14 +484,14 @@ class relation (object):
return len(self.content) - l return len(self.content) - l
class header(tuple): class Header(tuple):
'''This class defines the header of a relation. '''This class defines the header of a relation.
It is used within relations to know if requested operations are accepted''' It is used within relations to know if requested operations are accepted'''
# Since relations are mutalbe we explicitly block hashing them # Since relations are mutalbe we explicitly block hashing them
def __new__(cls, fields): def __new__(cls, fields):
return super(header, cls).__new__(cls, tuple(fields)) return super(Header, cls).__new__(cls, tuple(fields))
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
'''Accepts a list with attributes' names. Names MUST be unique''' '''Accepts a list with attributes' names. Names MUST be unique'''
@ -504,7 +504,7 @@ class header(tuple):
raise Exception('Attribute names must be unique') raise Exception('Attribute names must be unique')
def __repr__(self): def __repr__(self):
return "header(%s)" % super(header, self).__repr__() return "Header(%s)" % super(Header, self).__repr__()
def rename(self, params): def rename(self, params):
'''Returns a new header, with renamed fields. '''Returns a new header, with renamed fields.
@ -520,7 +520,7 @@ class header(tuple):
attrs[id_] = new attrs[id_] = new
except: except:
raise Exception('Field not found: %s' % old) raise Exception('Field not found: %s' % old)
return header(attrs) return Header(attrs)
def sharedAttributes(self, other): def sharedAttributes(self, other):
'''Returns how many attributes this header has in common with a given one''' '''Returns how many attributes this header has in common with a given one'''
@ -537,3 +537,7 @@ class header(tuple):
def getAttributesId(self, param): def getAttributesId(self, param):
'''Returns a list with numeric index corresponding to field's name''' '''Returns a list with numeric index corresponding to field's name'''
return [self.index(i) for i in param] return [self.index(i) for i in param]
#Backwards compatibility
relation = Relation
header = Header

@ -26,7 +26,7 @@ import re
RELATION_NAME_REGEXP = r'^[_a-zA-Z]+[_a-zA-Z0-9]*$' RELATION_NAME_REGEXP = r'^[_a-zA-Z]+[_a-zA-Z0-9]*$'
class rstring (str): class Rstring (str):
'''String subclass with some custom methods''' '''String subclass with some custom methods'''
@ -112,7 +112,7 @@ class rstring (str):
return self._date return self._date
class rdate (object): class Rdate (object):
'''Represents a date''' '''Represents a date'''
@ -158,8 +158,11 @@ class rdate (object):
def __sub__(self, other): def __sub__(self, other):
return (self.intdate - other.intdate).days return (self.intdate - other.intdate).days
def is_valid_relation_name(name): def is_valid_relation_name(name):
'''Checks if a name is valid for a relation. '''Checks if a name is valid for a relation.
Returns boolean''' Returns boolean'''
return re.match(RELATION_NAME_REGEXP, name) != None return re.match(RELATION_NAME_REGEXP, name) != None
#Backwards compatibility
rdate = Rdate
rstring = Rstring

@ -18,7 +18,7 @@
from PyQt5 import QtCore, QtWidgets, QtWidgets from PyQt5 import QtCore, QtWidgets, QtWidgets
from relational import relation, parser, optimizer, rtypes from relational import relation, parser, optimizer, rtypes
from relational.maintenance import user_interface from relational.maintenance import UserInterface
from relational_gui import about from relational_gui import about
from relational_gui import survey from relational_gui import survey
@ -36,7 +36,7 @@ class relForm(QtWidgets.QMainWindow):
self.selectedRelation = None self.selectedRelation = None
self.ui = ui self.ui = ui
self.qcounter = 1 # Query counter self.qcounter = 1 # Query counter
self.user_interface = user_interface() self.user_interface = UserInterface()
self.settings = QtCore.QSettings() self.settings = QtCore.QSettings()