Example #1
0
 def start(self):
     Column.start(self)
     if (type(self.__function_config) == types.StringType and 
         string.count(self.__function_config,'as_node') == 1 and 
         self.__function_config.endswith('get')):
         func = self.__function_config
         self.__node = as_node(func[func.find('(')+2:func.rfind(')')-1])
         if self.use_subscription_manager:
             self._sid = SM.create_delivered(self, {1:as_node_url(self.__node)})
             self.function = self.get_last
         else:
             self.function = getattr(self.__node,func[func.rfind('.')+1:])
     rexec = self.parent.parent.get_environment()
     self.original_function = RFunction(self.function, args=self.args,
                                        context=self.context,
                                        rexec=rexec)
     self.function = self._convert
     self.variables = {}
     nodes = self.children_nodes()
     for potential_calculator in nodes:
         if hasattr(potential_calculator, 'evaluate'):
             if self._calculator: #oops
                 raise EAttributeError('Too many calculator nodes', self)
             self._calculator = potential_calculator
             self.function = self._evaluate # hook the calculator in
     self.__original_function = self.original_function
     self.original_function = self.__evaluate_original_function
     self.__started = 1
    def __init__(self, parent):
        super().__init__(parent)

        self.__parent__ = parent
        self.parent_model = parent.model
        self.partslist = parent.partslist
        self.rank_title = 'rank'
        self.quantities_key = 'quantity'
        self.key_column = 'key'

        self.ui = Ui_Dialog()
        self.ui.setupUi(self)

        # list view
        self.model_rank = Model(self, Item(), Column())
        self.ui.listView.setModel(self.model_rank)
        self.ui.listView.setItemDelegate(Delegate())

        # list view 2
        self.model_quantity = Model(self, Item(), Column())
        self.ui.listView_2.setModel(self.model_quantity)
        self.ui.listView_2.setItemDelegate(Delegate())

        # list view 3
        self.model_key = Model(self, Item(), Column())
        self.ui.listView_3.setModel(self.model_key)
        self.ui.listView_3.setItemDelegate(Delegate())

        # table view
        self.model_csv = Model(self, Item(), Column())
        self.ui.tableView.setModel(self.model_csv)
        self.ui.tableView.setItemDelegate(Delegate())

        self.ui.pushButton.clicked.connect(self.open_file)
Example #3
0
 def start(self):
     Column.start(self)
     if (type(self.__function_config) == types.StringType
             and string.count(self.__function_config, 'as_node') == 1
             and self.__function_config.endswith('get')):
         func = self.__function_config
         self.__node = as_node(func[func.find('(') + 2:func.rfind(')') - 1])
         if self.use_subscription_manager:
             self._sid = SM.create_delivered(self,
                                             {1: as_node_url(self.__node)})
             self.function = self.get_last
         else:
             self.function = getattr(self.__node,
                                     func[func.rfind('.') + 1:])
     rexec = self.parent.parent.get_environment()
     self.original_function = RFunction(self.function,
                                        args=self.args,
                                        context=self.context,
                                        rexec=rexec)
     self.function = self._convert
     self.variables = {}
     nodes = self.children_nodes()
     for potential_calculator in nodes:
         if hasattr(potential_calculator, 'evaluate'):
             if self._calculator:  #oops
                 raise EAttributeError('Too many calculator nodes', self)
             self._calculator = potential_calculator
             self.function = self._evaluate  # hook the calculator in
     self.__original_function = self.original_function
     self.original_function = self.__evaluate_original_function
     self.__started = 1
Example #4
0
def p_column(p):
    r"""column : ID
                | table DOT ID"""
    if len(p) == 2:
        p[0] = Column.from_name(p[1])
    elif len(p) == 4:
        p[0] = Column.from_name_table(p[3], p[1])
Example #5
0
def reader(filename, ignoring, scheme=None):
    #os.chdir(os.path.sep.join(os.path.sep.split(filename)[:-1]))
    os.chdir(filename)

    if scheme is None:
        try:
            scheme = read_scheme(filename)
        except:
            scheme = {}
    if not os.path.exists(filename + '.zis'):
        fil = csv.reader(open(filename + '.csv'))
    else:
        fil = csv.reader(open(filename + '.zis'))  #need to implement pulke ish
    for i, line in enumerate(fil):
        if i == 0:
            columns = [[title] for title in line]
        elif ignoring <= i:
            for j, item in enumerate(line):
                columns[j].append(item)

    columnlist = []
    if scheme != {}:
        for item in columns:
            if len(item) > 1:
                columnlist.append(
                    Column(item[0], scheme.type_by_field(item[0]), item[1:]))
            else:
                columnlist.append(
                    Column(item[0], scheme.type_by_field(item[0]), []))

        table = Table(columnlist)
        os.chdir("..")
        return table
    os.chdir("..")
    return columnlist
Example #6
0
 def createRandomTable(cls, name, nrow, ncol, ncolstr=0,
       low_int=0, hi_int=100, table_cls=None):
   """
   Creates a table with random integers as values
   Input: name - name of the table
          nrow - number of rows
          ncol - number of columns
          ncolstr - number of columns with strings
          low_int - smallest integer
          hi_int - largest integer
          table_cls - Table class to use; default is Table
   """
   if table_cls is None:
     table_cls = cls
   ncol = int(ncol)
   nrow = int(nrow)
   table = cls(name)
   ncolstr = min(ncol, ncolstr)
   ncolint = ncol - ncolstr
   c_list = range(ncol)
   random.shuffle(c_list)
   for n in range(ncol):
     column = Column("Col_" + str(n))
     if c_list[n] <= ncolint - 1:
       values = np.random.randint(low_int, hi_int, nrow)
       values_ext = values.tolist()
     else:
       values_ext = ut.randomWords(nrow)
     #values_ext.append(None)
     column.addCells(np.array(values_ext))
     table.addColumn(column)
   table.setFilepath(settings.SCISHEETS_DEFAULT_TABLEFILE)
   return table
Example #7
0
 def _get_rel_data_restricted(self, sql):
     saved = self._restricted_table_call
     table_name, relation, pk, pk_value = saved['table_name'], saved['relation'], saved['pk'], saved['pk_value']
     relation_fk = Structure.get_foreign_key_for_table(relation, table_name)
     if not self._cache.relation_exists(table_name, relation):
         if sql._select_args:
             sql.add_select_arg(Column(relation, relation_fk))
         if sql._limit:
             union_sql = []
             union_parameters = []
             union_dict = {}
             for id in self._cache.get_all_keys(table_name, pk):
                 limiting_sql = copy.deepcopy(sql)
                 limiting_sql.add_where_literal(Column(relation, relation_fk) == id)
                 union_dict = limiting_sql.build_select() 
                 union_sql.append('(%s)' % union_dict['sql'])
                 union_parameters.extend(union_dict['parameters'])
             union_dict['sql'], union_dict['parameters'] = ' UNION '.join(union_sql), union_parameters
             data = Query().execute_and_fetch(**union_dict)
             self._cache.save_relation(table_name, relation, data)
         else:
             sql.add_where_literal(Column(relation, relation_fk).in_(self._cache.get_all_keys(table_name, pk)))
             data = Query().execute_and_fetch(**sql.build_select())
             self._cache.save_relation(table_name, relation, data)
     return ResultSet(self._cache.get_relation_set(relation, relation_fk, pk_value), relation, self._cache)
Example #8
0
 def stop(self):
     self.__started = 0
     Column.stop(self)
     self.variables = None
     self._calculator = None
     if self._sid:
         SM.destroy(self._sid)
         self._sid = None
Example #9
0
class WebRating(Table):
    """The table specified in Task 3"""
    table_name = 'test'

    id = Column('id', Integer())
    url = Column('url', String())
    date = Column('date', DateTime())
    rating = Column('rating', String())
Example #10
0
 def stop(self):
     self.__started = 0
     Column.stop(self)
     self.variables = None
     self._calculator = None
     if self._sid:
         SM.destroy(self._sid)
         self._sid = None
Example #11
0
 def set_header_list(self, header_list):
     for column in header_list:
         if isinstance(column, Column):
             self.header_list.append(column)
         elif isinstance(column, dict):
             self.header_list.append(Column.from_dict(column))
         else:
             self.header_list.append(Column(column))
     self.header_list = header_list
Example #12
0
 def __init__(self):
     self.function = None
     self._calculator = None
     self.__node = None
     self.__node_url = None
     self.__lock = Lock()
     self.__started = 0
     self._sid = None
     self._present_value = None
     Column.__init__(self)
     EventConsumerMixin.__init__(self, self.change_of_value)
Example #13
0
    def test_get_column_value(self):
        col1 = Column("rrr", 0, "str", "sample1")
        col2 = Column("abc", 1, "int", "sample1")
        col3 = Column("xyz", 2, "str", "sample1")

        cols = [col1, col2, col3]
        line = ["text for col1", 99, "text for col3"]

        self.assertEqual(main.get_column_value(cols, line, "rrr"), "text for col1")
        self.assertEqual(main.get_column_value(cols, line, "abc"), 99)
        self.assertEqual(main.get_column_value(cols, line, "xyz"), "text for col3")
Example #14
0
 def __init__(self):
     self.function = None
     self._calculator = None
     self.__node = None
     self.__node_url = None
     self.__lock = Lock()
     self.__started = 0
     self._sid = None
     self._present_value = None
     Column.__init__(self)
     EventConsumerMixin.__init__(self, self.change_of_value)
Example #15
0
 def find_columns(self):
     undone_prices = self.prices.copy()
     while len(undone_prices) > 0:
         price1 = undone_prices.pop(0)
         column = Column(price1)
         for price2 in undone_prices.copy():
             angle = get_angle(price1.top_right, price2.top_right)
             if get_abs_perp_angle_diff(self.angle,
                                        angle) < self.COLUMN_ANGLE_TRESHOLD:
                 undone_prices.remove(price2)
                 column.add(price2)
         self.columns.append(column)
     self.columns.sort(
         key=lambda column_: column_.get_rotated_x(self.angle))
 def read_data_from_text_file(self, file_path):
     with open(file_path, 'r') as f:
         num_types = int(f.readline().strip())
         f.readline()
         for num_type in range(num_types):
             semantic_type = f.readline().strip()
             column = Column(str(num_type), file_path)
             column.semantic_type = "---".join(
                 [part.split("/")[-1] for part in semantic_type.replace("#", "").split("|")])
             num_values = int(f.readline())
             for num_val in range(num_values):
                 column.add_value(f.readline().split(" ", 1)[1])
             f.readline()
             self.column_map[column.name] = column
 def read_data_from_xml(self, file_path):
     xml_tree = ElementTree.parse(file_path)
     root = xml_tree.getroot()
     for child in root:
         for attrib_name in child.attrib.keys():
             if attrib_name not in self.column_map:
                 column = Column(attrib_name, file_path)
                 self.column_map[attrib_name] = column
             self.column_map[attrib_name].add_value(child.attrib[attrib_name])
         for attrib in child:
             if attrib.tag not in self.column_map:
                 column = Column(attrib.tag, file_path)
                 self.column_map[attrib.tag] = column
             self.column_map[attrib.tag].add_value(attrib.text)
Example #18
0
 def configuration(self):
     config = Column.configuration(self)
     get_attribute(self, 'context', config)
     get_attribute(self, 'conversion', config, _name)
     config['function'] = self.__function_attribute
     get_attribute(self, 'args', config)
     return config
Example #19
0
def get_columns_from_file_data(file_lines: List[List[str]], file_has_headers: bool) -> List[Column]:
    """
    Extracts the columns information from the file and automatically sets their name, type and nullable according
    to the corresponding data
    :param file_lines: All the lines from the csv already interpreted as a 2D table of strings
    :param file_has_headers: If the file has headers in the first line
    :return: The list of interpreted columns
    """
    first_line = file_lines[0]
    first_data_line = file_lines[1] if file_has_headers else file_lines[0]
    cols = []

    for index, data in enumerate(first_line):
        col_name = first_line[index] if file_has_headers else "col{}".format(index + 1)
        col_data = first_data_line[index]

        if re.match(r"^[1-9]\d*([,.]\d+)?$", col_data):
            data_type = "number"
        elif re.match(r"\d{2,4}[-\/]\d{2,4}[-\/]\d{2,4}", col_data):
            data_type = "date"
        else:
            data_type = "str"
        cols.append(Column(col_name, index, data_type, first_data_line[index]))

    return cols
Example #20
0
    def Edit_Card(self, P_Key, P_New_Title, P_New_Description):
        for Column in self.Columns.values():
            if (Column.Edit_Card(P_Key, P_New_Title,
                                 P_New_Description) == True):
                return True

        return False
Example #21
0
    def create_measure(self, composition, measure):
        '''
        Creates one printable measure and returns it as X * 16 matrix, where X is the number of columns,
        which is determined by the shortest note duration. e.g. if shortest note is 1/8, measure consists of 8 columns.
        '''

        shortest = 1
        for note in composition.notes:  # e.g. if shortest note/rest is 1/8, measure contains 8 columns
            if measure == note.measure:  #
                if note.duration < shortest:  #
                    shortest = note.duration  #
        for rest in composition.rests:
            if measure == rest.measure:  #
                if rest.duration < shortest:  #
                    shortest = rest.duration  #

        columns = int(1 / shortest) * [None]  # list of columns in this measure

        for i in range(len(columns)):  # for each column in measure
            col = Column(composition, measure,
                         (shortest * i) + shortest)  # create new column
            columns[i] = col  # add to the list of columns of this measure

        measure_matrix = [[None] * 16 for i in range(len(columns))
                          ]  # create meatrix for the whole measure
        for j in range(16):  #
            for i in range(len(columns)):  #
                measure_matrix[i][j] = columns[i].rows[
                    j]  # fill matrix according to list of columns

        return measure_matrix
Example #22
0
 def __iter__(self):
     '''Will return an iterable of all columns'''
     self.columns = []
     subpath = os.path.join(self.file_name, '*')
     for column_file in glob.glob(subpath):
         self.columns.append(iter(Column(column_file)))
     return self
Example #23
0
 def addColumn(self, column, index=None):
   """
   Adds a column to the table.
   Adjusts the Column length to that of the table
   :param column: column object
   :param int index: position for the new column
   :return: error text if there is a problem with the column
                   None if no problem
   Notes: (1) A new column may have either no cells
              or the same number as the existing table
   """
   error = None
   # Check for problems with this column
   is_ok = all([c.getName(is_global_name=False) 
       != column.getName(is_global_name=False) 
       for c in self.getChildren()])
   if not is_ok:
     error = "**%s is a duplicate name" % column.getName()
     return error
   else:
     error = Column.isPermittedName(  \
         column.getName(is_global_name=False))
     if error is not None:
       return error
   if index is None:
     index = len(self.getColumns(is_attached=False))
   # Handle the different cases of adding a column
   self.addChild(column, position=index)
   # Case 1: First column after name column
   if self.numColumns() == 1:
     self._updateNameColumn()
   # Case 2: Subsequent columns
   else:
     self.adjustColumnLength()
   self._validateTable()
Example #24
0
    def __init__(self):
        '''Defining the class attributes here'''

        #In case the user makes an invalid move, the error attribute will show the user direction to do it again.
        self.error = "\nThis is an invalid move. Look at the board carefully and play again.\n"

        #Board_full attribute shows if the board is completely full or not.
        self.board_full = False

        #Creating a list in order to store board data
        self.board_list = []

        #Creating 7 positions to create the row of the board
        for i in range(7):

            #Storing the column information into the board list
            self.board_list.append(Column(i))

        #Winner attribute shows if there is a winner in the game or not
        self.winner = False

        #Chip_color attribute shows whose turn it is at the moment, default is red, meaning the player with red chips will start making move first.
        self.chip_color = 'R'

        #Creating a base board to begin with
        self.output_list = [['.', '.', '.', '.', '.', '.'],
                            ['.', '.', '.', '.', '.', '.'],
                            ['.', '.', '.', '.', '.', '.'],
                            ['.', '.', '.', '.', '.', '.'],
                            ['.', '.', '.', '.', '.', '.'],
                            ['.', '.', '.', '.', '.', '.'],
                            ['.', '.', '.', '.', '.', '.']]
Example #25
0
 def configuration(self):
     config = Column.configuration(self)
     get_attribute(self,'context',config)
     get_attribute(self,'conversion',config,_name)
     config['function'] = self.__function_attribute
     get_attribute(self,'args',config)
     return config
Example #26
0
	def parseCreateTableString(self, createTableString):
		createTablePattern = re.compile('CREATE TABLE `(?P<name>[a-z_]+)` \((?P<columns>.*?)\) ENGINE=(?P<engine>[a-z]+) (AUTO_INCREMENT=(?P<autoincrement>\d+) )?DEFAULT CHARSET=(?P<charset>[a-z\d]+)',
				re.IGNORECASE | re.DOTALL)
		matches = createTablePattern.match(createTableString)

		if matches is None:
			print "Error:\n" + createTableString

		columns = matches.group('columns').strip().split("\n")
		for index, column in enumerate(columns):
			column = column.strip()
			column = column.strip(',')

			primaryKeyMatch = re.match("^(PRIMARY KEY \((?P<columns>.*)\))", column)
			uniqueKeyMatch = re.match("^(UNIQUE KEY `(?P<key_name>.*?)` \((?P<columns>.*)\))", column)
			keyMatch = re.match("^(KEY `(?P<key_name>.*?)` \((?P<columns>.*)\))", column)

			if primaryKeyMatch is not None:
				indexColumns = self.columnStringsToObjects(Index.parseColumnNamesFromString(primaryKeyMatch.group('columns')))
				self.indexes.append(Index('PRIMARY', 'PRIMARY', indexColumns))
			elif uniqueKeyMatch is not None:
				indexColumns = self.columnStringsToObjects(Index.parseColumnNamesFromString(uniqueKeyMatch.group('columns')))
				self.indexes.append(Index('UNIQUE', uniqueKeyMatch.group('key_name'), indexColumns))
			elif keyMatch is not None:
				indexColumns = self.columnStringsToObjects(Index.parseColumnNamesFromString(keyMatch.group('columns')))
				self.indexes.append(Index('KEY', keyMatch.group('key_name'), indexColumns))
			else:
				self.columns.append(Column.fromString(column))

		self.name = matches.group('name')
		self.autoincrement = matches.group('autoincrement')
Example #27
0
    def __init__(self, app):
        super().__init__()

        self.copy_data = None
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.resize(800, 600)

        self.model = Model(self, Item(),
                           Column(['File name', 'Resolution', 'Settings']))

        self.ui.tableView.setModel(self.model)
        self.ui.tableView.setItemDelegate(Delegate())
        self.ui.tableView.clicked.connect(self.tableview_clicked)
        self.ui.tableView.customContextMenuRequested.connect(
            self.context_menu_tableview)

        self.tool_bar = ToolBar(self.ui.toolBar)
        self.ui.toolBar.addWidget(self.tool_bar)

        self.tool_bar.ui.toolButton.clicked.connect(self.open_files)
        self.tool_bar.ui.toolButton_3.clicked.connect(
            self.ui.image_view.recognize)
        self.tool_bar.ui.toolButton_4.clicked.connect(self.split_cells)
        self.tool_bar.ui.toolButton_5.clicked.connect(
            self.ui.image_view.graphics_view_update)
        self.tool_bar.ui.toolButton_6.clicked.connect(
            self.ui.image_view.graphics_view_update)
        self.tool_bar.ui.toolButton_7.clicked.connect(
            self.ui.image_view.graphics_view_update)
        self.tool_bar.ui.toolButton_8.clicked.connect(self.ocr)
Example #28
0
 def get_table_structure(self, table_name):
     """
     获取指定表结构信息
     """
     with self.db.cursor() as cursor:
         cursor.execute(f"""select t.TABLE_NAME     AS tableName,
                t.COLUMN_NAME    AS columnName,
                c.COMMENTS       AS columnComment,
                t.NULLABLE       AS nullable,
                t.DATA_DEFAULT,
                t.DATA_TYPE      AS dataType,
                t.CHAR_LENGTH    AS strLength,
                t.DATA_PRECISION AS numLength,
                t.DATA_SCALE     AS numBit
               from user_tab_columns t, user_col_comments c
              where t.TABLE_NAME = c.TABLE_NAME
                and t.COLUMN_NAME = c.COLUMN_NAME
                and t.TABLE_NAME = '{table_name}'
              order by t.TABLE_NAME, t.COLUMN_ID""")
         columns = []
         rows = cursor.fetchall()
         for row in rows:
             columns.append(
                 Column(column_name=row[1].lower(),
                        comments=row[2],
                        nullable=row[3],
                        data_default=row[4],
                        datatype=row[5],
                        char_length=row[6],
                        data_precision=row[7],
                        data_scale=row[8]))
         return columns
Example #29
0
 def get_column_by_id(self, id):
     (status, result) = self._send_template_request('getColumn',
                                                    {'column_id': id})
     if status:
         return Column(self, result)
     else:
         return None
Example #30
0
 def __init__(self, name, path, gtfsPath, cols, exists):
     self.name = name
     self.path = path
     self.gtfsPath = gtfsPath
     self.exists = exists
     self.columns = {}
     for col in cols:
         self.columns[col] = Column(col, self)
Example #31
0
async def get_select(request: Request):
    # todo: skal ikke behøve alias
    req = Dict(
        {item[0]: item[1]
         for item in request.query_params.multi_items()})
    cnxn = Connection(cfg.db_system, cfg.db_server, cfg.db_uid, cfg.db_pwd,
                      req.base)  #TODO
    dbo = Database(cnxn, req.base)
    tbl = Table(dbo, req.table)
    if 'key' in req:
        key = json.loads(req.key)
        colname = key[-1]
    else:
        colname = self.get_primary_key()[-1]
    col = Column(tbl, colname)
    data = col.get_select(req)
    return data
Example #32
0
    def init_fields(self, config=None):
        """Store Dict of fields in table object"""
        if (self.db.metadata.get("cache", None) and not config):
            self.cache.fields = self.db.metadata.cache.tables[self.name].fields
            return
        fields = Dict()
        indexes = self.get_indexes()
        cols = self.get_columns()
        for col in cols:
            colnames = [column[0] for column in col.cursor_description]
            col = Dict(zip(colnames, col))
            if ('column_size' in col or 'display_size' in col):
                col.column_size = col.get('column_size', col.display_size)
            cname = col.column_name

            column = Column(self, cname)
            fields[cname] = column.get_field(col)

            if config:
                # Find if column is (largely) empty

                threshold = int(config.threshold) / 100

                sql = f"""
                select count(*) from {self.name}
                where {cname} is not null
                """

                cursor = self.db.cnxn.cursor()
                count = cursor.execute(sql).fetchval()
                if (self.rowcount and count / self.rowcount < threshold):
                    fields[cname].hidden = True

        updated_idx = indexes.get(self.name + "_updated_idx", None)
        if updated_idx:
            for col in updated_idx.columns:
                fields[col].extra = "auto_update"
                fields[col].editable = False
        created_idx = indexes.get(self.name + "_created_idx", None)
        if created_idx:
            for col in created_idx.columns:
                fields[col].extra = "auto"
                fields[col].editable = False

        self.cache.fields = fields
Example #33
0
File: table.py Project: davekr/pypg
 def select(self, *args):
     if args:
         map(lambda arg: self._check_is_instance(arg, 'Column'), args)
         args = list(args)
         args.append(
             Column(self._table_name,
                    Structure.get_primary_key(self._table_name)))
         self._sql.add_select_args(args)
     return self._table_selected_instance()
Example #34
0
 def _get_fk_data(self, table_name, fk, fk_value):
     reltable = Structure.get_fk_referenced_table(table_name, fk)
     reltable_pk = Structure.get_primary_key(reltable)
     if not self._cache.relation_exists(table_name, reltable):
         sql = SQLBuilder(reltable)
         sql.add_where_literal(Column(reltable, reltable_pk).in_(self._cache.get_all_keys(table_name, fk)))
         data = Query().execute_and_fetch(**sql.build_select())
         self._cache.save_relation(table_name, reltable, data)
     return Row(self._cache.get_relation_row(reltable, reltable_pk, fk_value), reltable, self)
Example #35
0
    def create_table(self):
        if os.path.exists(self.name):
            if self.ine: return
            else: raise TableExistsError(self.name)
        self.table = Table([Column(header,typ,[]) for (header,typ) in list(self.fields.items())])

        os.mkdir(self.name)
        os.chdir(self.name)
        reader.write(self.name,self.table)
Example #36
0
 def dataframeToTable(self, table_name, dataframe, names=None):
   """
   Creates a Table from the pandas dataframe.
   :param str table_name: name of the table
   :param pd.DataFrame dataframe:
   :param list-of-str names: names of names in the dataframe
                               that are names in the table.
                               Defaull is all.
   :return Table table:
   """
   if names is None:
     names = list(dataframe.columns)
   table = Table(table_name)
   for name in names:
     column = Column(name)
     column.addCells(dataframe[name], replace=True)
     table.addColumn(column)
   return table 
Example #37
0
	def list_columns(self, table_name):
		columns_list = []
		cur = self.db.cursor()
		# get lists of column_name, type, charLen for CHAR, precision for numeric entries, nullable
		query_string = "SELECT column_name, data_type, character_maximum_length, numeric_precision, \
										is_nullable from information_schema.columns WHERE table_name = %s;"
		cur.execute(query_string, (table_name,))
		columns_tuple = cur.fetchall()
		
		# http://stackoverflow.com/questions/12379221
		cur.execute("SELECT k.COLUMN_NAME \
			FROM information_schema.table_constraints t \
			INNER JOIN information_schema.key_column_usage k \
			USING ( constraint_name, table_schema, table_name )  \
			WHERE t.constraint_type =  'PRIMARY KEY' \
			AND t.table_name = %s;",  (table_name,))
		# fetch list of primary keys	
		columns_prim_tuple = cur.fetchall()
		
		#http://www.tocker.ca/2013/05/02/fastest-way-to-count-rows-in-a-table.html
		#cur.execute("SELECT TABLE_ROWS FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = %s",  (table_name,))
		cur.execute("SELECT COUNT(*) FROM {0}".format (table_name))
		
		row_count = cur.fetchone()
		
		cur.close()
		
		# traverese tuple of tuples to list of strings
		# http://stackoverflow.com/questions/1663807
		for col, col_prim in itertools.izip_longest(columns_tuple, columns_prim_tuple):
			# create Column object
			column = Column()
			# convert tuple item into string
			col = list(col)
			col[0] = col[0].strip("(),'")
			
			# col_prim is None, do not append assign None
			if col_prim:
				col_prim = list(col_prim)
				col_prim[0] = col_prim[0].strip("(),'")
				column.primary_key = col_prim[0]
			else:
				column.primary_key = ''
			
			# populate column object
			column.name = col[0]
			column.type = col[1]
			column.charLen = col[2]
			column.precision = col[3]
			column.nullable = "NULL" if col[4] == "YES" else "NOT NULL"
			columns_list.append(column)
		
		return columns_list, row_count[0]
Example #38
0
	def list_columns(self, table_name):
		columns_list = []
		cur = self.conn.cursor()
		# get lists of column_name, type, charLen for CHAR, precision for numeric entries, nullable
		# http://stackoverflow.com/questions/2146705
		cur.execute("SELECT column_name, data_type, character_maximum_length, numeric_precision, is_nullable from information_schema.columns \
										WHERE table_name = %s;", (table_name,))
		columns_tuple = cur.fetchall()
		
		cur.execute("SELECT a.attname FROM  pg_index i \
			JOIN  pg_attribute a ON a.attrelid = i.indrelid \
			AND a.attnum = ANY(i.indkey) \
			WHERE  i.indrelid = %s::regclass \
			AND  i.indisprimary;",  (table_name,))
			
		columns_prim_tuple = cur.fetchall()
		
		#https://wiki.postgresql.org/wiki/Count_estimate
		#cur.execute("SELECT reltuples FROM pg_class WHERE oid = %s::regclass; ",  (table_name,))
		cur.execute("SELECT COUNT(*) FROM %s;",  (AsIs(table_name),))
		row_count = cur.fetchone()

		cur.close()
		# traverese tuple of tuples to list of strings
		# http://stackoverflow.com/questions/1663807
		for col, col_prim in itertools.izip_longest(columns_tuple, columns_prim_tuple):
			# create Column object
			column = Column()
			
			col = list(col)
			col[0] = col[0].strip("(),'")
			
			# col_prim is None, do not append assign None
			if col_prim:
				col_prim = list(col_prim)
				col_prim[0] = col_prim[0].strip("(),'")
				column.primary_key = col_prim[0]
			else:
				column.primary_key = ''
			
			column.name = col[0]
			column.type = col[1]
			column.charLen = col[2]
			column.precision = col[3]
			column.nullable = "NULL" if col[4] == "YES" else "NOT NULL"
			columns_list.append(column)
			
		#row_count = list(row_count_tuple)
		return columns_list, row_count[0]
Example #39
0
 def configure(self,config):
     Column.configure(self,config)
     set_attribute(self,'context','None',config,str)
     set_attribute(self,'function', REQUIRED, config)
     set_attribute(self, 'use_subscription_manager', 1, config, int)
     ##
     # @fixme HACK to work around too much voodoo to fix right now.
     self.__function_attribute = self.function
     set_attribute(self,'conversion',as_magnitude,config,_function)
     self.original_function = self.function
     if type(self.function) == types.StringType:
         self.function = string.replace(
             self.function, 'self.',
             'as_internal_node("%s").' % as_node_url(self)
             )
     set_attribute(self,'args','()',config)
     # fix for bad configuration
     if self.args == '':
         self.args = '()'
     self.__function_config = self.function
     self._last_time = None
     self._last_value = None
     self._period = self.parent.parent.period
Example #40
0
 def addColumnsToTableFromDataframe(self, 
                                    dataframe, 
                                    names=None, 
                                    column_position=None):
   """
   Adds columns from a dataframe to the table. If a column of the same
   name exists, its data is replaced.
   :param pandas.DataFrame dataframe:
   :param list-of-str names: names of columns in the dataframe
       to include. Default (None) is all.
   :param str column_position: name of the column to place after
   :return list-of-str names: names of columns added to the table
   """
   self.updateColumnFromColumnVariables()  # Make sure table is current
   if names is None:
     names = list(dataframe.columns)
   if column_position is None:
     index = self._table.numColumns()
   else:
     column = self._table.columnFromName(column_position)
     index = self._table.indexFromColumn(column) + 1
   for name in names:
     if self._table.isColumnPresent(name):
       column = self._table.columnFromName(name)
     else:
       if "." in name:
         import pdb; pdb.set_trace()
       column = Column(name)
       self._table.addColumn(column, index=index)
       index += 1
     column.addCells(dataframe[name], replace=True)
     if column.getTable() is None:
       import pdb; pdb.set_trace()
       pass
   self.setColumnVariables()
   return names
 def test_validate_numeric(self):
     self.assertTrue(Column.validate('numeric', 10.1))
     self.assertTrue(Column.validate('numeric', 100))
     self.assertTrue( not Column.validate('numeric', 'teste'))
def column_test():
    start = time.time()
    column = Column("http://zhuanlan.zhihu.com/booooks")
    column.parser()
    print "Title of the column is ", column.get_title()
    print "Author of the column is ", column.get_author()
    print "Author id of the column is ", column.get_author_id()
    print "Number of follower of the column is ", column.get_follower_num()
    print "The description of the column is ", column.get_description()
    print "The number of posts of the column is ", column.get_post_count()
    column.save_all_posts()
    column.save_all_followers_profile()  # might be time-consuming
    end = time.time()
    print "Time used is", end - start
 def test_validate_varchar(self):
     self.assertTrue(Column.validate('varchar', 'teste'))
     self.assertTrue( not Column.validate('varchar', 100))
     self.assertTrue( not Column.validate('varchar', 10.1))
 def test_validate_bigint(self):
     self.assertTrue(Column.validate('bigint', 100))
     self.assertTrue( not Column.validate('bigint', 10.1))
     self.assertTrue( not Column.validate('bigint', 'teste'))
Example #45
0
  leave = PID_LEAVE.match(message)
  if leave:
    pid = leave.group(2)
    package_line = leave.group(1)
    if match_packages(package_line) and pid in pids:
      return pid, package_line
  death = PID_DEATH.match(message)
  if death:
    pid = death.group(2)
    package_line = death.group(1)
    if match_packages(package_line) and pid in pids:
      return pid, package_line
  return None, None

columns = OrderedDict()
columns['tag'] = Column(max_length=args.tag_width, align=Column.ALIGN_RIGHT,
                        always_first_line=args.always_tags)
columns['level'] = Column(max_length=1,
                          always_first_line=args.always_tags)
level_padding = 1 # 1
message_width = width - len(columns) - (2*level_padding) - sum(map(
  lambda col: col.max_length, columns.itervalues()))
columns['message'] = Column(max_length=message_width)

def all_columns_are_done(columns):
  return all([col.is_done() for col in columns.itervalues()])

def print_message_columns(columns, bg=WHITE):
  buf = ''
  while not all_columns_are_done(columns):
    for name,col in columns.iteritems():
      if name == 'padding':
Example #46
0
 def get_column(self, column_index):
     assert 0 <= column_index < COLUMNS_MAX
     column = Column(self._pattern_id, column_index)
     column.set_controller(self._controller)
     return column