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

  • Home
  • SEARCH
  • 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 8759339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:46:39+00:00 2026-06-13T14:46:39+00:00

I am using PyContract (not PyContracts ) to implement design-by-contracts for my genetic algorithms

  • 0

I am using PyContract (not PyContracts) to implement design-by-contracts for my genetic algorithms framework.

The main module is GA.py, which calls a function from crossover.py (let’s call it foocross). I have written precondition and postcondition expressions for foocross.

Now, I would like those conditions to be checked when I run the main in GA.py. I introduced a false precondition in foocross just to test if the contracts were being checked, but it seems that they are not. How do I make sure that the contracts are being checked?

Here’s a minimal section of my code:

# GA.py
def main():
    # some code
    crossover.foocross(params)
    # some more code

if __name__ == "__main__":
    main()

#crossover.py
def injectionco(p1, p2, chrom):
    """
        pre:
            isinstance(p1, int) # this should raise an error. p1 is never an int
            isinstance(p2, list)
            isinstance(chrom, int)
            forall(p1, lambda elem: any(isinstance(elem, i.__class__) for i in p2))
            forall(p2, lambda elem: any(isinstance(elem, i.__class__) for i in p1))
            len(p1) == len(p2)

        post:
            p1 is __old__.p1
            p2 is __old__.p2
            p1 == __old__.p1
            p2 == __old__.p2
            isinstance(__return__, p1.__class__)    # returns an individual
            len(__return__) == len(p1)
            id(__return__) not in [id(p1), id(p2)]
            forall(__return__, lambda elem: __return__.count(elem) == 1)
        """

    #code
    return #something

PS: this was a pretty difficult question for me to write because I had to strip out a LOT of my code (since the framework is pretty complex) and I’m new to design-by-contract. So if I’ve missed out on some information that would help answer this question, please ask and I’ll happily provide more info 🙂

  • 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-06-13T14:46:41+00:00Added an answer on June 13, 2026 at 2:46 pm

    Looking at the example, seems like all you need to do would be:

    # this needs to be there unconditionally.
    import contract
    contract.checkmod(__name__)
    
    if __name__ == '__main__':
       import doctest, GA
       doctest.testmod(GA)
    

    However, this below will NOT work (because inside import GA, contract.checkmod() does not happen):

    if __name__ == '__main__':
       import contract
       contract.checkmod(__name__)
       import doctest, GA
       doctest.testmod(GA)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on writing some constraints for a class method using PyContract (not PyContracts
Using Delphi 2010. I am looking for (possibly) a function or procedure which can
I'm trying to expose a webHttpBinding EndPoint using Framework 4.0. <endpoint address=web binding=webHttpBinding contract=MyContract/>
Using MVC2 I have an AJAX form which is posting to a bound model.
Using MVVM. I have a DataTemplate which I am using to display an expander
Using Yii, I want to delete all the rows that are not from today.
Using android 2.3.3, I have a background Service which has a socket connection. There's
I've recently been trying to generate data contracts from xsd files, using svcutil like
Using CMake I want to check if a particular function (cv::getGaborKernel) from OpenCV library
Using The Play Framework how easy would it be to allow an admin user

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.