带Pi的预设变量

2024-10-01 00:31:25 发布

您现在位置:Python中文网/ 问答频道 /正文

    import time
    from random import randint
    import pickle

    MaTC = 1
    MaTC = pickle.dump(MaTC, open("MaTCFile.txt", "wb"))

    AnTC = 2
    AnTC = pickle.dump(AnTC, open("AnTCFile.txt", "wb"))

    AuTC = 3
    AuTC = pickle.dump(AuTC, open("AuTCFile.txt", "wb"))

    AlTC = 3
    AlTC = pickle.dump(AlTC, open("AlTCFile.txt", "wb"))

    JaTC = 3
    JaTC = pickle.dump(JaTC, open("JaTCFile.txt", "wb"))

    print("Hello Jacob")
    time.sleep(1)
    print("Choose an option!")
    print("(1) Selecting Cleaners")
    print("(2) Edit Cleaning Information")
    print("(3) Reserved")
    MenuChoice = input(": ")
    if MenuChoice == "1":
        print("Selecting Cleaner Function")

    elif MenuChoice == "2":
        print("(1) Check cleaning info")
        print("(2) Enter New Cleaning info")
        InfoSelect = input(": ")
            if InfoSelect == "2":

            Mare = { "Mares Times Cleaned": MaTC}
            Andre = { "Andres Times Cleaned": AnTC}
            Al = { "Als Times Cleaned": AlTC}
            Austin = { "Austins Times Cleaned": AuTC}
            Jacob = { "Jacobs Times Cleaned": JaTC}
            pickle.dump( Mare, open ("MaresFile.txt", "wb"))
            pickle.dump( Jacob, open ("JacobsFile.txt", "wb"))
            pickle.dump( Andre, open ("AndresFile.txt", "wb"))
            pickle.dump( Austin,open ("AustinsFile.txt", "wb"))
            pickle.dump( Al, open ("AlsFile.txt", "wb"))
            print(Mare)
            print(Austin)
            print(Jacob)
            print(Andre)
            print(Al)

好的,基本上我要实现的是在第一次运行程序时,将MaTC,AnTC,AuTC,AlTC,和JaTC变量预设为1,2,3,3和3。但是当我想加2表示MaTC使其为3时,当我再次启动程序时,我希望它在启动时等于3。我几天前刚开始使用python,我很想得到反馈!你知道吗


Tags: importtxtopendumppickleprintwbtimes
2条回答

使用异常处理文件不存在的情况(即程序的第一次运行)

try:
  f = open("MaTCFile.dat", "rb") # Pickle is not a .txt format
  MaTC = pickle.load(f)
except IOError:
  # Initialise default value
  MaTC = 1

我也倾向于使用dict将所有变量存储在一个文件中:

default_data = {
  "MaTC": 1, "AnTC": 2, # ... etc.

因为这将使程序更易于管理。你知道吗

这是你程序的重构版本。它使用pickle模块并演示pickletoolszlib和其他一些模块的用法。希望这些代码能帮助您进一步编写程序。你将来可能想研究的一个课题是数据库。你知道吗

import os
import pickle
import pickletools
import sys
import zlib


SETTINGS_FILE = 'settings.sav'


def main():
    """Present the user with a cleaning application."""
    settings = load_settings(SETTINGS_FILE)
    print('Hello, Jacob!')
    while True:
        if show_menu(settings,
                     select_cleaners,
                     edit_cleaning_information,
                     reserved,
                     close_program):
            break
    settings.save(SETTINGS_FILE)


def load_settings(path):
    """Create the settings schema and load them from file is possible."""
    settings = Namespace(mares=Parameter(1, is_positive_int),
                         andres=Parameter(2, is_positive_int),
                         austin=Parameter(3, is_positive_int),
                         al=Parameter(3, is_positive_int),
                         jacob=Parameter(3, is_positive_int))
    if os.path.isfile(path):
        settings.load(path)
    return settings


def is_positive_int(value):
    """Ensure that the value is valid for the settings."""
    return isinstance(value, int) and value >= 0


def show_menu(context, *args):
    """Help display a menu to the user."""
    for option, function in enumerate(args, 1):
        print(option, '-', function.__doc__)
    while True:
        number = get_int('Please enter an option: ') - 1
        if 0 <= number < len(args):
            return args[number](context)
        else:
            print('Your number was out of range.')


def get_int(prompt):
    """Get a valid number from the user."""
    while True:
        try:
            text = input(prompt)
        except EOFError:
            sys.exit()
        else:
            try:
                return int(text)
            except ValueError:
                print('You did not enter an integer.')


def select_cleaners(context):
    """Choose this to select your cleaner."""
    print('Selecting Cleaning Function')


def edit_cleaning_information(context):
    """You can edit the cleaning information."""
    show_menu(context, check_cleaning_info, enter_new_cleaning_info)


def reserved(context):
    """This is reserved for future use."""
    print('NOT AVAILABLE')


def close_program(context):
    """Close the program."""
    return True


def check_cleaning_info(context):
    """Show all cleaning info."""
    print('Mares:', context.mares)
    print('Andres:', context.andres)
    print('Austin:', context.austin)
    print('Al:', context.al)
    print('Jacob:', context.jacob)


def enter_new_cleaning_info(context):
    """Enter in additional cleaning information."""
    while True:
        name = input('Who needs cleaning info adjusted? ').capitalize()
        if name == 'Mares':
            context.mares += get_int('Add to Mares: ')
        elif name == 'Andres':
            context.andres += get_int('Add to Andres: ')
        elif name == 'Austin':
            context.austin += get_int('Add to Austin: ')
        elif name == 'Al':
            context.al += get_int('Add to Al: ')
        elif name == 'Jacob':
            context.jacob += get_int('Add to Jacob: ')
        else:
            continue
        break

###############################################################################


class _Settings:

    """_Settings(*args, **kwargs) -> _Settings instance"""

    @staticmethod
    def _save(path, obj):
        """Save an object to the specified path."""
        data = zlib.compress(pickletools.optimize(pickle.dumps(obj)), 9)
        with open(path, 'wb') as file:
            file.write(data)

    @staticmethod
    def _load(path):
        """Load an object from the specified path."""
        with open(path, 'rb') as file:
            data = file.read()
        return pickle.loads(zlib.decompress(data))


class Namespace(_Settings):

    """Namespace(**schema) -> Namespace instance"""

    def __init__(self, **schema):
        """Initialize the Namespace instance with a schema definition."""
        self.__original, self.__dynamic, self.__static, self.__owner = \
            {}, {}, {}, None
        for name, value in schema.items():
            if isinstance(value, _Settings):
                if isinstance(value, Namespace):
                    if value.__owner is not None:
                        raise ValueError(repr(name) + 'has an owner!')
                    value.__owner = self
                self.__original[name] = value
            else:
                raise TypeError(repr(name) + ' has bad type!')

    def __setattr__(self, name, value):
        """Set a named Parameter with a given value to be validated."""
        if name in {'_Namespace__original',
                    '_Namespace__dynamic',
                    '_Namespace__static',
                    '_Namespace__owner',
                    'state'}:
            super().__setattr__(name, value)
        elif '.' in name:
            head, tail = name.split('.', 1)
            self[head][tail] = value
        else:
            attr = self.__original.get(name)
            if not isinstance(attr, Parameter):
                raise AttributeError(name)
            attr.validate(value)
            if value == attr.value:
                self.__dynamic.pop(name, None)
            else:
                self.__dynamic[name] = value

    def __getattr__(self, name):
        """Get a Namespace or Parameter value by its original name."""
        if '.' in name:
            head, tail = name.split('.', 1)
            return self[head][tail]
        if name in self.__dynamic:
            return self.__dynamic[name]
        attr = self.__original.get(name)
        if isinstance(attr, Namespace):
            return attr
        if isinstance(attr, Parameter):
            return attr.value
        raise AttributeError(name)

    __setitem__ = __setattr__
    __getitem__ = __getattr__

    def save(self, path):
        """Save the state of the entire Namespace tree structure."""
        if isinstance(self.__owner, Namespace):
            self.__owner.save(path)
        else:
            self._save(path, {Namespace: self.state})

    def load(self, path):
        """Load the state of the entire Namespace tree structure."""
        if isinstance(self.__owner, Namespace):
            self.__owner.load(path)
        else:
            self.state = self._load(path)[Namespace]

    def __get_state(self):
        """Get the state of this Namespace and any child Namespaces."""
        state = {}
        for name, types in self.__static.items():
            box = state.setdefault(name, {})
            for type_, value in types.items():
                box[type_] = value.state if type_ is Namespace else value
        for name, value in self.__original.items():
            box = state.setdefault(name, {})
            if name in self.__dynamic:
                value = self.__dynamic[name]
            elif isinstance(value, Parameter):
                value = value.value
            else:
                box[Namespace] = value.state
                continue
            box.setdefault(Parameter, {})[type(value)] = value
        return state

    def __set_state(self, state):
        """Set the state of this Namespace and any child Namespaces."""
        dispatch = {Namespace: self.__set_namespace,
                    Parameter: self.__set_parameter}
        for name, box in state.items():
            for type_, value in box.items():
                dispatch[type_](name, value)

    def __set_namespace(self, name, state):
        """Set the state of a child Namespace."""
        attr = self.__original.get(name)
        if not isinstance(attr, Namespace):
            attr = self.__static.setdefault(name, {})[Namespace] = Namespace()
        attr.state = state

    def __set_parameter(self, name, state):
        """Set the state of a child Parameter."""
        attr = self.__original.get(name)
        for type_, value in state.items():
            if isinstance(attr, Parameter):
                try:
                    attr.validate(value)
                except TypeError:
                    pass
                else:
                    if value == attr.value:
                        self.__dynamic.pop(name, None)
                    else:
                        self.__dynamic[name] = value
                    continue
            if not isinstance(value, type_):
                raise TypeError(repr(name) + ' has bad type!')
            self.__static.setdefault(name, {}).setdefault(
                Parameter, {})[type_] = value

    state = property(__get_state, __set_state, doc='Namespace state property.')


class Parameter(_Settings):

    """Parameter(value, validator=lambda value: True) -> Parameter instance"""

    def __init__(self, value, validator=lambda value: True):
        """Initialize the Parameter instance with a value to validate."""
        self.__value, self.__validator = value, validator
        self.validate(value)

    def validate(self, value):
        """Check that value has same type and passes validator."""
        if not isinstance(value, type(self.value)):
            raise TypeError('Value has a different type!')
        if not self.__validator(value):
            raise ValueError('Validator failed the value!')

    @property
    def value(self):
        """Parameter value property."""
        return self.__value


###############################################################################


if __name__ == '__main__':
    main()

相关问题 更多 >