Home All elements in list of defined class variables get the same value when making assigments
Reply: 0

All elements in list of defined class variables get the same value when making assigments

user5177 Published in June 20, 2018, 10:44 pm

I have defined a class called Container and inherits list to hold data from and XML file which containing debian package names, public keys, repositories and other data which is later used in the script.

I initialize a class variable as a list of this type and later in the code I want to fill it up but when assigning new values, all other already assign elements change their value to the newly assig one.

Disclaimer: I may be over complicating my code, but I'm doing it on purpose to get more practice classes.

Class definition --> GetFromXML.py


import os
import xml.etree.ElementTree as ET

class GetFromXML2:
    This is version 2 of the  class GetFromXML above.
    For description, scroll up to the beginning of code.
    def __init__(self, data_path=os.path.join(os.path.dirname(__file__), 'data.xml')):
        self._data_path = data_path
        if self._data_path == os.path.join(os.path.dirname(__file__), 'data.xml'):
            print('No path passed during object initialization, therefore default path shown below was used:\n{}'.format(self._data_path))
            print('A new path was passed during object initialization, the path being used is shown below:\n{}'.format(self._data_path))
        self._data = [Container]
        self._root = None
        print('Initialization complete, object fully initialized.')

    def load_data(self):
        print('Loading data from {}.'.format(self._data_path))
        self._root = ET.parse(self._data_path).getroot()
        for i, k in enumerate(self._root.iter('key')):
            self.add(i, k.attrib['install'], k.text)

    def add(self, index, install, tag):
        if index != 0:
            self._data.insert(index, Container)
        self._data[index].tag = tag
        self._data[index].install = install

class Container(list):
    def __init__(self):
        self._install = False
        self._tag = ''

    def tag(self):
        return self._tag

    def tag(self, value):
        self._tag = value

    def install(self):
        return self._install

    def install(self, value):
        self._install = value

main --> nubty.py

This is the main script that runs all of my Ubuntu personalization.

__version__ = 0.1

import os
import GetFromXML

def main():

    print('nubt version: {}'.format(__version__))
    print('Started running {} script.'.format(__file__))

    dir = os.path.dirname(os.path.realpath(__file__))
    path = os.path.join(dir, 'data.xml')

    # Send path as parameter for GetFromXML to the data XML, or else it will
    # default to data.xml in the same dir as this script.
    xml = GetFromXML.GetFromXML2()

if __name__ == '__main__': main()

IDE snapshot during debugging.

Notice variable data self._data[idx].tag equal to all others no matter the index.

XML data file

<?xml version="1.0" encoding="UTF-8"?>
        <key install="False">https://dl-ssl.google.com/linux/linux_signing_key.pub</key>
        <key install="False">https://dl.sinew.in/keys/enpass-linux.key</key>
        <key install="False">https://dl.winehq.org/wine-builds/Release.key</key>
        <key install="True">https://www.virtualbox.org/download/oracle_vbox_2016.asc</key>
        <repository install="False" >deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main</repository>
        <repository install="False" >deb http://repo.sinew.in/ stable main</repository>
        <repository install="True" >deb https://download.virtualbox.org/virtualbox/debian bionic contrib</repository>
        <ppa install="False" >ppa:webupd8team/gnome3</ppa>
        <ppa install="False" >ppa:notepadqq-team/notepadqq</ppa>
        <ppa install="True" >https://dl.winehq.org/wine-builds/ubuntu/</ppa>
        <package install="False">google-chrome-stable</package>
        <package install="False">notepadqq</package>
        <package install="False">dconf-editor</package>
        <package install="False">nfs-common</package>
        <package install="False">enpass</package>
        <package install="True">virtualbox-5.2</package>
        <package install="False">virtualbox-guest-additions-iso</package>
        <entry install="True">#Synology NFS shares</entry>
    <!--Downloads - this section is to downland files from websites that don't have apt packages-->


You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.307121 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO