Skip to main content

A python package for creating logic circuits.

Project description

Circuits-py

A python package for creating logic circuits.

see examples

from circuits_py import State

class DLatch():
    def __init__(self, Q, QI):
        self.Q = Q
        self.QI = QI
    def d_latch(self, Clock, D):
        self.Q = State(D).NOT().AND(Clock).NOR(self.QI).state
        self.QI = State(D).AND(Clock).NOR(self.Q).state

l = DLatch(1, 0)

while True:
    l.d_latch(1, 0)
from circuits_py import State, logic, divide, combine

def addCircuit(a, b, carry):
    a = State(a)  # A State object represents current up to the next logic gate.

    divide(a.XOR(b),  # when an circuit path divides
                lambda e: e.XOR(carry),
                lambda e: a.AND(carry).OR(a.AND(b).state)
            )

addCircuit(1, 1, 0)

Design

using plain functions to create circuits make it harder to understand and write:

out = AND(0, AND(0, XOR(input, 0))) # gates are nested
#     <----<----<----<----<-----
#     the circuit flows in the opposite direction of the code

instead:

State(1).XOR(0).AND(0).AND(0)
#     ----->---->---->---->
#     the circuit flows in the direction of the code

State

A State object represents path of electrical current up to the next logical gate, a state can be initialized either 0 or 1.

a = State(0)
b = State(1)

supported logic functions: AND, OR, XOR, NOT, NOR, NAND, XNOR and BUFFER

when a logic gate is invoked a new State() object is returned initialized output of that gate

State(1).XOR(0).AND(1)  # returns State(1)

the output state can be accessed by get_output()

print(State(1).XOR(0).AND(1).get_output()) # prints 1

Custom Logic

use the combine() function to use custom logic,

from circuits_py import combine, logic_gate

@logic_gate
def custom_logic(A, B):
    if ((A) and (not B)):
      return True
    else:
      return False

# combine() takes two or more paths and combines it to a logic function

combine(      
  State(1).AND(1),
  State(0).AND(0),      
    Logic=custom_logic  # pass the logic function
)

combine()

combine two or more paths to a logic gate

example:

@logic_gate
def OR(A, B):
    return A or B

combine(
    State(b).NOT().AND(a),  # source
    State(a).NOT().AND(b),  # source
        Logic=OR   # Logic function
)

def combine(*sources, Logic=None)

  • *sources State() objects,
  • Logic a logic function which takes *source parameters and returns a bool output (the function should use the @logic_gate decorator)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

circuits_py-0.1.1.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

circuits_py-0.1.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file circuits_py-0.1.1.tar.gz.

File metadata

  • Download URL: circuits_py-0.1.1.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for circuits_py-0.1.1.tar.gz
Algorithm Hash digest
SHA256 4acbec0b3c64efe557d79fbd6be07261d6a6645250a20ff0e9ee6aa7442cca0e
MD5 e7818c9ddda5ac23b5396d2960bb0d80
BLAKE2b-256 d4ebecdb2836d78a9dfe716c882516048fa5e3e839bfcaaa6a623659e376dcc1

See more details on using hashes here.

File details

Details for the file circuits_py-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: circuits_py-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for circuits_py-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1f068cc7782fbcfbcf1b7fc97a14bbf90bcf39d02b5a2d052d71396cb5fc6386
MD5 a2f28e54f94b7c8cd0fa8166c26d164b
BLAKE2b-256 c5e9a5c4c5f74e58caaeaa5d87fa54c14b14fe09bb9c4d61f0d3eba901d2c4f0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page