Commit 11b842d0 authored by Michele Maris's avatar Michele Maris
Browse files

Merge branch 'main' of https://www.ict.inaf.it/gitlab/michele.maris/yapsut into main

parents 962a4c97 660048cd
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -2,3 +2,4 @@
*.*~
*~
*__pycache__*
*.ipyn*
+81 −0
Original line number Diff line number Diff line
%% Cell type:markdown id:a79b0eaa tags:

# Cookies

M.Maris - 2026 Jun 14 -

usage of Cookies

%% Cell type:code id:73b19d27 tags:

``` python
from yapsut.cookies import cookie, cookies_collection
```

%% Cell type:code id:f843337d tags:

``` python
cc={'name':'ciao',
    'version':'0.1.0',
    'author':'mm',
    'context_libraries':'--none--',
    'context_parameters':'--none--',
    'description':'prints ciao',
    'code':"""print('ciao')"""}
cc=cookie(cc)
print(cc.keys())

cc.to_dict()

mcc=cookies_collection()
mcc['ciao']=cc

mcc['bella']={'name':'bella',
    'version':'0.1.0',
    'author':'mm',
    'context_libraries':'--none--',
    'context_parameters':'--none--',
    'description':'prints bella',
    'code':"""print('bella')"""}

mcc['one']={'name':'one',
    'version':'0.1.0',
    'author':'mm',
    'context_libraries':'--none--',
    'context_parameters':'foo1 : the number to be printed\nfoo2 : the number to be set',
    'description':'prints foo1 and sets foo2',
    'code':"""
print('foo1 ',foo1)
foo2=10+foo1
"""}

mcc.keys()

print(mcc['one'].code)


foo1=1

exec(mcc['one'].code)
print('foo2 is ',foo2)
```

%% Output

    ['name', 'version', 'author', 'context_libraries', 'context_parameters', 'description', 'code']
    <class 'yapsut.cookies.cookie'> <class 'yapsut.cookies.cookie'> True
    <class 'dict'> <class 'yapsut.cookies.cookie'> False
    <class 'dict'> <class 'yapsut.cookies.cookie'> False
    print('foo1 ',foo1)
    foo2=10+foo1
    foo1  1
    foo2 is  11

%% Cell type:markdown id:6cdcd4ef-dd76-4cdb-89b4-259772b0bc2b tags:

# End of Document

%% Cell type:code id:b5dd9061-be6a-4202-8660-00718d8cb150 tags:

``` python
```

src/yapsut/cookies.py

0 → 100644
+226 −0
Original line number Diff line number Diff line
__description__="""cookies for repeated operations
they are operated within the current context with `eval` or `exec`
"""

class cookie :
    _keys=['name','version','author','context_libraries','context_parameters','description','code']
    #
    @property
    def name(self) :
        """coockie name"""
        return self._name
    @name.setter
    def name(self,this) :
        """coockie name"""
        self['name']=this
    #
    @property
    def version(self) :
        """version"""
        return self._version
    @version.setter
    def version(self,this) :
        """version"""
        self['version']=this
    #
    @property
    def author(self) :
        """author"""
        return self._author
    @author.setter
    def author(self,this) :
        """author"""
        self['author']=this
    #
    @property
    def context_libraries(self) :
        """libraries needed from context"""
        return self._context_libraries
    @context_libraries.setter
    def context_libraries(self,this) :
        """libraries needed from context"""
        self['context_libraries']=this
    #
    @property
    def context_parameters(self) :
        """parameters from/to context"""
        return self._context_parameters
    @context_parameters.setter
    def context_parameters(self,this) :
        """parameters from/to context"""
        self['context_parameters']=this
    #
    @property
    def description(self) :
        """description"""
        return self._description
    @description.setter
    def description(self,this) :
        """description"""
        self['description']=this
    #
    @property
    def code(self) :
        """code"""
        return self._code
    @code.setter
    def code(self,this) :
        """code"""
        self['code']=this
    #
    def __init__(self,*arg) :
        self.clear()
        if len(arg) > 0 :
            if type(arg[0]) == type({}) :
                self.from_dict(arg[0])
    #
    def clear(self) :
        """resets the cookie"""
        for this in self._keys :
            self.__dict__[f'_{this}']=None
    #
    def copy(self) :
        """makes an hard copy"""
        import copy
        return copy.deepcoy(self)
    #
    def keys(self) :
        return self._keys
    #
    def __setitem__(self,this,that) :
        if not this in self._keys :
            raise Exception(f'{this} is not a valid parameter')
        if type(that) != type("") :
            raise Exception(f'content of {this} must be a string')
        if type(that.strip()) == "" :
            raise Exception(f'content of {this} must be not an empty string')
        self.__dict__[f'_{this}']=that.strip()
    #
    def __getitem__(self,this) :
        if not this in self._keys :
            raise Exception(f'{this} is not a valid parameter')
        return self.__dict__[f'_{this}']
    #
    def from_dict(self,this) :
        """load a dictionary"""
        for k in this.keys() :
            self[k]=this[k]
    #
    def to_dict(self) :
        """saves a dictionary"""
        out={}
        for k in self.keys() :
            out[k]=self[k]
        return out
    #
    def banner(self) :
        """creates a banner"""
        out=[]
        for k in self.keys() :
            line=f'{k}:\n"""{self[k]}"""\n'
            out.append(line)
        return '\n'.join(out)
    #
    def __repr__(self) :
        x=self['name']
        return f'<{str(self.__class__)}: {x}>'
    #
    def eval(self) :
        """eval"""
        eval(self._code)

class cookies_collection :
    """a class to handle a collection of cookies"""
    #
    @property
    def name(self) :
        """name"""
        return self._name
    @name.setter
    def name(self,this) :
        """name"""
        if type(this) != type("") :
            raise Exception('name must be a string')
        self._name=this.strip()
    #
    @property
    def version(self) :
        """version"""
        return self._version
    @version.setter
    def version(self,this) :
        """version"""
        if type(this) != type("") :
            raise Exception('version must be a string')
        self._version=this.strip()
    #
    @property
    def description(self) :
        """description"""
        return self._description
    @description.setter
    def description(self,this) :
        """description"""
        if type(this) != type("") :
            raise Exception('description must be a string')
        self._description=this.strip()
    #
    def __init__(self,name='',version='',description='') :
        self.clear()
        self._name=name
        self._version=version
        self._description=description
    #
    def clear(self) :
        """resets the cookie"""
        self._name=''
        self._version=''
        self._description=''
        self._list={}
    #
    def copy(self) :
        """makes an hard copy"""
        import copy
        return copy.deepcoy(self)
    #
    def keys(self) :
        return self._list.keys()
    #
    def __setitem__(self,this,that) :
        if type(that) != type({}) and type(that) != type(cookie()) :
            raise Exception(f'content of {this} must be a dictionary or a cookie')
        if type(that) == type({}) :
            ic=cookie()
            ic.from_dict(that)
        else :
            ic=that
        if this is None :
            n=ic.name
        elif type(this)!=type('') :
            raise Exception('this must be a string')
        elif this.strip() == '' :
            n=ic.name
        else :
            n=this.strip()
        self._list[n]=ic
    #
    def __getitem__(self,this) :
        if type(this) == type(int) :
            if this < 0 or this >=len(self) :
                raise Exception(f'{this} out of range')
            else :
                k=list(self.keys())[this]
                return self[k]
        if not type(this) == type('') :
            raise Exception(f'`this` must be a string')
        if not this in self.keys() :
            raise Exception(f'{this} is not in the list')
        return self._list[this]
    #
    def __len__(self) :
        """number of elements in list"""
        return len(self._list)
    #
    def __repr__(self) :
        return f'<{self.__class__}[{len(self)}]: {self._name}>'