Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 539615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:09:15+00:00 2026-05-13T10:09:15+00:00

This works now for those new to this question: class ensureparams(object): Used as a

  • 0

This works now for those new to this question:

class ensureparams(object):
    """

    Used as a decorator with an iterable passed in, this will look for each item
    in the iterable given as a key in the params argument of the function being
    decorated. It was built for a series of PayPal methods that require
    different params, and AOP was the best way to handle it while staying DRY.


    >>> @ensureparams(['name', 'pass', 'code'])
    ... def complex_function(params):
    ...     print(params['name'])
    ...     print(params['pass'])
    ...     print(params['code'])
    >>> 
    >>> params = {
    ...     'name': 'John Doe',
    ...     'pass': 'OpenSesame',
    ...     #'code': '1134',
    ... }
    >>> 
    >>> complex_function(params=params)
    Traceback (most recent call last):
        ...
    ValueError: Missing from "params" dictionary in "complex_function": code
    """
    def __init__(self, required):
        self.required = set(required)

    def __call__(self, func):
        def wrapper(*args, **kwargs):
            if not kwargs.get('params', None):
                raise KeyError('"params" kwarg required for {0}'.format(func.__name__))
            missing = self.required.difference(kwargs['params'])
            if missing:
                raise ValueError('Missing from "params" dictionary in "{0}": {1}'.format(func.__name__, ', '.join(sorted(missing))))
            return func(*args, **kwargs)
        return wrapper

if __name__ == "__main__":
    import doctest
    doctest.testmod()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:09:16+00:00Added an answer on May 13, 2026 at 10:09 am

    What I did was add *args, **kwargs, and just check for the keys that are required within the ‘params’ argument via kwargs[‘params’] after checking that kwargs params exists.

    Here’s the new version (which works perfectly):

    class requiresparams(object):
        """
    
        Used as a decorator with an iterable passed in, this will look for each item
        in the iterable given as a key in the params argument of the function being
        decorated. It was built for a series of PayPal methods that require
        different params, and AOP was the best way to handle it while staying DRY.
    
    
        >>> @requiresparams(['name', 'pass', 'code'])
        ... def complex_function(params):
        ...     print(params['name'])
        ...     print(params['pass'])
        ...     print(params['code'])
        >>> 
        >>> params = {
        ...     'name': 'John Doe',
        ...     'pass': 'OpenSesame',
        ...     #'code': '1134',
        ... }
        >>> 
        >>> complex_function(params=params)
        Traceback (most recent call last):
            ...
        ValueError: Missing from "params" dictionary: code
        """
        def __init__(self, required):
            self.required = set(required)
    
        def __call__(self, func):
            def wrapper(*args, **kwargs):
                if not kwargs.get('params', None):
                    raise KeyError('"params" kwarg required for {0}'.format(func.__name__))
                missing = self.required.difference(kwargs['params'])
                if missing:
                    raise ValueError('Missing from "params" dictionary: %s' % ', '.join(sorted(missing)))
                return func(*args, **kwargs)
            return wrapper
    
    if __name__ == "__main__":
        import doctest
        doctest.testmod()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How come this works: import datetime now = datetime.datetime.now() month = '%d' % now.month
I use the wonderful jQuery-plugin Galleriffic to display image galeries. Now this works great,
EDIT: This code now works correctly, I only left it in case someone finds
This works: scope :archived, :conditions => day_id IN (#{Day.where(year_id != #{DateTime.now.year}).collect{ |d| d.id }.join(,)})
I'm wondering how to best tackle this, since what I have now works great
I am stumped right now. In my last post about this question the answer
i am trying to use this code: <%= File.ReadAllText(Server.MapPath(Members/newsletters/welcome.html))%> which works great but now
We have been discussing this at work and now I am even more confused
now I can make it work with user-profile.tpl.php page,all this page does is to
Initially I thought this was going to work, but now I understand it won't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.