I registered PyQCheck, a library that can perform QuickCheck with Python, in PyPI.

PyQCheck

PyQCheck released at the end of August last year. Released for Python 2.7 all the time, and made setup.py I left it in the repository, so I thought it was a waste, so I took the following actions.

--Compatible with Python 3.3 (previous 3.1, 3.2 and 2.x series are not guaranteed) --Registration with PyPI (simplification of installation) --Slight details are supported (code porting to init.py, which I think is a strange response-> directory import measures ...)

It has only poor features, but I think it can be used reasonably well.

How it was created

I forgot the details, but the library called QuickCheck got excited in a very small area in a certain place. People who have different favorite languages can use their favorite programming language in a QuickCheck style. I think it was because I created a framework that enables testing.

What to do

Simply put, it behaves as follows

--Create your own function that takes arguments. --Enter and execute a value that guarantees the range that can be processed for that argument and guarantees the type. --Determine whether the value returned by the function is True or False, or whether it throws an exception

Let's use it for the time being

Operating environment

There are the following conditions as the operating environment

--Python 3.3 or higher must be installed

Installation

You can install it with the following command. ... Use pyvenv to prepare the installation environment to prevent system environment pollution. Drop distribute and put easy_install to prepare for installation.

> mkdir ~/Sandbox/PyQCheck
> cd ~/Sandbox/PyQCheck
> pyvenv .venv
> source .venv/bin/activate
> curl -O http://python-distribute.org/distribute_setup.py
> python distribute_setup.py
> easy_install PyQCheck

This is the end of preparation.

Let's see how it works

The code snippet for the operation sample is attached below.

equal_length.py


# -*- coding:utf-8 -*-

from pyqcheck import PyQCheck, Arbitrary

def equal_length(a, b):
  return len(a) == len(b)

PyQCheck(verbose=True).add(
  Arbitrary(
    ('string', dict(min=10, max=10)), 
    ('string', dict(min=10, max=10))
  ).property(
    'len(a) == len(b)', equal_length
  )
).run(10).result()

Run

When you run it, it will be output in the following form.

pyqcheck-1.png

What are you doing

  1. For functions where Arbitrary takes two arguments, the minimum length is 10, the maximum is 10, that is, 10 characters. Pass a string
  2. equal_length is passed a string to a, b
  3. Returns True because it is a string of the same length
  4. As a result of trying the test 10 times, everything is OK, so it is filled with the ☀ icon

After all

For the function to be tested, as processing of the automatic argument received by the function Appropriately generate a value that specifies the supported type and data length, etc. Does it pass the test properly? Can be checked. Try 100 times, 1000 times, 10000 times, and if you pass, it will be OK, Feeling like that. (Although it may be different from the original idea of QuickCheck ...)

Change the writing style

You can also write using a decorator.

equal_length.py


# -*- coding:utf-8 -*-

from pyqcheck import PyQCheck, set_arbitrary

@set_arbitrary(
  ('string', dict(min=10, max=10)), 
  ('string', dict(min=10, max=10))
)
def equal_length(a, b):
  '''
  len(a) == len(b)
  '''
  return len(a) == len(b)

PyQCheck(verbose=True).run(10).result()

I think this way of writing is cleaner ...

Unusual usage

You can also use the following as an unusual usage of PyQCheck. Let's run the following script. For execution, you have to build Python3 with the link with sqlite3 enabled.

insert_random_value_to_the_sqlite.py


# -*- coding:utf-8 -*-

import sqlite3
from pyqcheck import PyQCheck, set_arbitrary

DB_NAME = 'test.db'

@set_arbitrary(
  ('integer', dict(min=0, max=100)),
  ('string', dict(min=3, max=10))
)
def insert_to_sqlite3(age, name):
  con = sqlite3.connect(DB_NAME)
  cur = con.cursor()
  cur.execute('''
    SELECT `name` FROM `sqlite_master` 
      WHERE `type`='table' AND name='users';
  ''')

  if cur.fetchone() is None:
    cur.execute('''
      CREATE TABLE `users` (
        `id` INTEGER NOT NULL PRIMARY KEY,
        `age` INTEGER NOT NULL,
        `name` STRING NOT NULL
      );
    ''')
    con.commit()

  cur.execute('''
    INSERT INTO `users` (`age`, `name`) 
      VALUES (?, ?)
  ''', (age, name))
  con.commit()
  
  return True

PyQCheck(verbose=True).run(30).result()

Run

Try to run it.

pyqcheck-2.png

Since test.rb is created in the current directory, let's take a look at the contents ...

pyqcheck-3.png

Taking advantage of the property of generating a textual value of PyQCheck and passing it to a function, It is said that it was used to generate dummy data. The states of the United States, the names of countries in the world, Try creating commonly used first and last name data as Arbitrary If you do that, I feel that it can be used to generate dummy data.

from now on

--Allow users and users to create their own defined Arbitrary --Aiming for continuous release --Output to file

I registered the package on PyPI, so I want to continue maintenance.

Recommended Posts

I registered PyQCheck, a library that can perform QuickCheck with Python, in PyPI.
I made a familiar function that can be used in statistics with Python
I made a package that can compare morphological analyzers with Python
About psd-tools, a library that can process psd files in Python
I made a shuffle that can be reset (reverted) with Python
I made a module PyNanaco that can charge nanaco credit with python
A memo that I wrote a quicksort in Python
I tried to create a class that can easily serialize Json in Python
I want to create a priority queue that can be updated in Python (2.7)
I want to work with a robot in python.
A memo that I touched the Datastore with python
Publish / upload a library created in Python to PyPI
I made a simple typing game with tkinter in Python
Published a library that hides character data in Python images
Let's create a script that registers with Ideone.com in Python.
I tried "a program that removes duplicate statements in Python"
I made a puzzle game (like) with Tkinter in Python
I made a plug-in that can "Daruma-san fell" with Minecraft
A story that went missing when I specified a path starting with a tilde (~) in python open
I made a program to collect images in tweets that I liked on twitter with Python
I made a fortune with Python.
I made a daemon with Python
[Python] Make a graph that can be moved around with Plotly
I made a library to easily read config files with Python
[Python] I made my own library that can be imported dynamically
I want to use a wildcard that I want to shell with Python remove
Use networkx, a library that handles graphs in python (Part 2: Tutorial)
Created a library for python that can easily handle morpheme division
[Python] A memo that I tried to get started with asyncio
Make a Spinbox that can be displayed in Binary with Tkinter
[For beginners] How to register a library created in Python in PyPI
Make a Spinbox that can be displayed in HEX with Tkinter
I made a library that adds docstring to a Python stub file.
[python] I made a class that can write a file tree quickly
I bought and analyzed the year-end jumbo lottery with Python that can be executed in Colaboratory
A story that didn't work when I tried to log in with the Python requests module
I wrote a tri-tree that can be used for high-speed dictionary implementation in D language and Python.
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
[Python] A program that creates stairs with #
Create a BOT that can call images registered with Discord like pictograms
I created a template for a Python project that can be used universally
Introducing a library that was not included in pip on Python / Windows
I made a payroll program in Python!
I made a web application in Python that converts Markdown to HTML
I made a character counter with Python
I drew a heatmap with seaborn [Python]
I made a Discord bot in Python that translates when it reacts
Try using APSW, a Python library that SQLite can get serious about
I tried to develop a Formatter that outputs Python logs in JSON
I tried a functional language with Python
[DSU Edition] AtCoder Library reading with a green coder ~ Implementation in Python ~
[Python] I made a utility that can access dict type like a path
What I did with a Python array
Try HTML scraping with a Python library
I made a Hex map with Python
A typed world that begins with Python
I made a tool that makes decompression a little easier with CLI (Python3)
[IOS] I made a widget that displays Qiita trends in Pythonista3. [Python]
Code reading of faker, a library that generates test data in Python
I created a password tool in Python.
I wrote FizzBuzz in python using a support vector machine (library LIVSVM).