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()
class user_interface (object):
class UserInterface (object):
'''It is used to provide services to the user interfaces, in order to
reduce the amount of duplicated code present in different user interfaces.

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

@ -26,7 +26,7 @@ from collections import deque
from relational.rtypes import *
class relation (object):
class Relation (object):
'''
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
self.content = set()
self.header = header([])
self.header = Header([])
return
with open(filename) as fp:
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()
attributes = len(self.header)
@ -121,7 +121,7 @@ class relation (object):
Selection, expr must be a valid Python expression; can contain field names.
'''
newt = relation()
newt.header = header(self.header)
newt.header = Header(self.header)
for i in self.content:
# Fills the attributes dictionary with the values of the tuple
attributes = {attr: i[j].autocast()
@ -148,7 +148,7 @@ class relation (object):
'Unable to perform product on relations with colliding attributes'
)
newt = relation()
newt.header = header(self.header + other.header)
newt.header = Header(self.header + other.header)
for i in self.content:
for j in other.content:
@ -178,7 +178,7 @@ class relation (object):
newt = relation()
# Create the header
h = (self.header[i] for i in ids)
newt.header = header(h)
newt.header = Header(h)
# Create the body
for i in self.content:
@ -213,7 +213,7 @@ class relation (object):
'''
other = self._rearrange(other) # Rearranges attributes' order
newt = relation()
newt.header = header(self.header)
newt.header = Header(self.header)
newt.content = self.content.intersection(other.content)
return newt
@ -224,7 +224,7 @@ class relation (object):
'''
other = self._rearrange(other) # Rearranges attributes' order
newt = relation()
newt.header = header(self.header)
newt.header = Header(self.header)
newt.content = self.content.difference(other.content)
return newt
@ -261,7 +261,7 @@ class relation (object):
'''
other = self._rearrange(other) # Rearranges attributes' order
newt = relation()
newt.header = header(self.header)
newt.header = Header(self.header)
newt.content = self.content.union(other.content)
return newt
@ -298,7 +298,7 @@ class relation (object):
# Creating the header with all the fields, done like that because order is
# needed
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
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
# needed
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
sid = self.header.getAttributesId(shared)
@ -484,14 +484,14 @@ class relation (object):
return len(self.content) - l
class header(tuple):
class Header(tuple):
'''This class defines the header of a relation.
It is used within relations to know if requested operations are accepted'''
# Since relations are mutalbe we explicitly block hashing them
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):
'''Accepts a list with attributes' names. Names MUST be unique'''
@ -504,7 +504,7 @@ class header(tuple):
raise Exception('Attribute names must be unique')
def __repr__(self):
return "header(%s)" % super(header, self).__repr__()
return "Header(%s)" % super(Header, self).__repr__()
def rename(self, params):
'''Returns a new header, with renamed fields.
@ -520,7 +520,7 @@ class header(tuple):
attrs[id_] = new
except:
raise Exception('Field not found: %s' % old)
return header(attrs)
return Header(attrs)
def sharedAttributes(self, other):
'''Returns how many attributes this header has in common with a given one'''
@ -537,3 +537,7 @@ class header(tuple):
def getAttributesId(self, param):
'''Returns a list with numeric index corresponding to field's name'''
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]*$'
class rstring (str):
class Rstring (str):
'''String subclass with some custom methods'''
@ -112,7 +112,7 @@ class rstring (str):
return self._date
class rdate (object):
class Rdate (object):
'''Represents a date'''
@ -158,8 +158,11 @@ class rdate (object):
def __sub__(self, other):
return (self.intdate - other.intdate).days
def is_valid_relation_name(name):
'''Checks if a name is valid for a relation.
Returns boolean'''
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 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 survey
@ -36,7 +36,7 @@ class relForm(QtWidgets.QMainWindow):
self.selectedRelation = None
self.ui = ui
self.qcounter = 1 # Query counter
self.user_interface = user_interface()
self.user_interface = UserInterface()
self.settings = QtCore.QSettings()