Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing bitcode. #1

Merged
merged 7 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion AUTHORS.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
The following organizations or individuals have contributed to this repo:

-
- Jono Yang
- Philippe Ombredanne
- Ayan Sinha Mahapatra
- Jay Kumar
101 changes: 50 additions & 51 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,59 +1,58 @@
A Simple Python Project Skeleton
================================
This repo attempts to standardize the structure of the Python-based project's
repositories using modern Python packaging and configuration techniques.
Using this `blog post`_ as inspiration, this repository serves as the base for
all new Python projects and is mergeable in existing repositories as well.
bitcode
==================
This repo is a pure python implementation for `intbitset <https://github.com/inveniosoftware-contrib/intbitset>`_.

.. _blog post: https://blog.jaraco.com/a-project-skeleton-for-python-projects/


Usage
=====

A brand new project
Installation
-------------------
.. code-block:: bash

git init my-new-repo
cd my-new-repo
git pull git@github.com:nexB/skeleton

# Create the new repo on GitHub, then update your remote
git remote set-url origin git@github.com:nexB/your-new-repo.git
Requirements
###################
* Python 3.8 or later

From here, you can make the appropriate changes to the files for your specific project.

Update an existing project
---------------------------
.. code-block:: bash

cd my-existing-project
git remote add skeleton git@github.com:nexB/skeleton
git fetch skeleton
git merge skeleton/main --allow-unrelated-histories

This is also the workflow to use when updating the skeleton files in any given repository.
pip install bitcode

More usage instructions can be found in ``docs/skeleton-usage.rst``.


Release Notes
=============

- 2022-03-04:
- Synchronize configure and configure.bat scripts for sanity
- Update CI operating system support with latest Azure OS images
- Streamline utility scripts in etc/scripts/ to create, fetch and manage third-party dependencies
There are now fewer scripts. See etc/scripts/README.rst for details

- 2021-09-03:
- ``configure`` now requires pinned dependencies via the use of ``requirements.txt`` and ``requirements-dev.txt``
- ``configure`` can now accept multiple options at once
- Add utility scripts from scancode-toolkit/etc/release/ for use in generating project files
- Rename virtual environment directory from ``tmp`` to ``venv``
- Update README.rst with instructions for generating ``requirements.txt`` and ``requirements-dev.txt``,
as well as collecting dependencies as wheels and generating ABOUT files for them.

- 2021-05-11:
- Adopt new configure scripts from ScanCode TK that allows correct configuration of which Python version is used.
Documentation
---------------------------
bitcode is a fallback library for intbitset, so the ``intbitset`` class and its methods
have same names and parameters.

Below listed are the implemented classes and methods.

Classes
##########

* ``intbitset``

Methods for Automaton class
###############################

* ``add``
* ``clear``
* ``copy``
* ``difference``
* ``difference_update``
* ``discard``
* ``isdisjoint``
* ``issuperset``
* ``issubset``
* ``remove``
* ``strbits``
* ``symmetric_difference``
* ``symmetric_difference_update``
* ``tolist``
* ``union``
* ``union_update``
* ``intersection``
* ``intersection_update``
* ``__and__``
* ``__eq__``
* ``__contains__``
* ``__len__``
* ``__iter__``
* ``__hash__``
* ``__str__``

For documentation please refer to: https://intbitset.readthedocs.io/en/latest/
15 changes: 10 additions & 5 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[metadata]
name = skeleton
name = bitcode
license = Apache-2.0
version = 1.0.0

# description must be on ONE line https://github.com/pypa/setuptools/issues/1390
description = skeleton
description = bitcode is a fallback pure Python library for intbitset.
long_description = file:README.rst
long_description_content_type = text/x-rst
url = https://github.com/nexB/skeleton
url = https://github.com/nexB/bitcode

author = nexB. Inc. and others
author_email = info@aboutcode.org
Expand All @@ -16,6 +16,11 @@ classifiers =
Intended Audience :: Developers
Programming Language :: Python :: 3
Programming Language :: Python :: 3 :: Only
Programming Language :: Python :: 3.6
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3.10
Topic :: Software Development
Topic :: Utilities

Expand Down Expand Up @@ -60,4 +65,4 @@ testing =
docs =
Sphinx == 5.1.0
sphinx-rtd-theme >= 0.5.0
doc8 >= 0.8.1
doc8 >= 0.8.1
2 changes: 0 additions & 2 deletions src/README.rst

This file was deleted.

Empty file added src/bitcode/__init__.py
Empty file.
196 changes: 196 additions & 0 deletions src/bitcode/bitcode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# bitcode is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/nexB/bitcode for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#

CFG_INTBITSET_ENABLE_SANITY_CHECKS = True


class intbitset:
def __init__(self, rhs=None, preallocate=-1, trailing_bits=0, sanity_checks=CFG_INTBITSET_ENABLE_SANITY_CHECKS,
no_allocate=0):
self.bitset = 0

if isinstance(rhs, int):
self.bitset = rhs
elif isinstance(rhs, intbitset):
self.bitset = rhs.bitset
elif isinstance(rhs, (list, set, frozenset)):
for value in rhs:
self.add(value)

self.preallocate = preallocate
self.trailing_bits = trailing_bits
self.sanity_checks = sanity_checks
self.no_allocate = no_allocate

def add(self, value):
"""
Add an element to a set.
This has no effect if the element is already present.
"""
if value < 0:
raise ValueError("Value can't be negative")
self.bitset |= 1 << value

def clear(self):
self.bitset = 0

def copy(self):
""" Return a shallow copy of a set. """
new = intbitset()
new.bitset = self.bitset
return new

def difference(self, *args):
""" Return a new intbitset with elements from the intbitset that are not in the others. """
new = intbitset(self.bitset)
for other in args:
new.bitset = (new.bitset ^ other.bitset) & self.bitset
return new

def difference_update(self, *args):
""" Update the intbitset, removing elements found in others. """
for other in args:
self.bitset &= (self.bitset ^ other.bitset)

def discard(self, value):
"""
Remove an element from a intbitset if it is a member.
If the element is not a member, do nothing.
"""
self.bitset &= ~(1 << value)

def isdisjoint(self, other):
""" Return True if two intbitsets have a null intersection. """
return self.intersection(*[other]).bitset == 0

def issuperset(self, other):
""" Report whether this set contains another set. """
return (self.bitset & other.bitset) == other.bitset

def issubset(self, other):
""" Report whether another set contains this set. """
return (self.bitset & other.bitset) == self.bitset

def remove(self, key):
"""
Remove an element from a set; it must be a member.
If the element is not a member, raise a KeyError.
"""
initial_bitset = self.bitset
self.discard(key)
if initial_bitset == self.bitset:
raise KeyError(f"{key} not in bitset")

def strbits(self):
"""
Return a string of 0s and 1s representing the content in memory
of the intbitset.
"""
return bin(self.bitset)[2:]

def symmetric_difference(self, other):
"""
Return the symmetric difference of two sets as a new set.
(i.e. all elements that are in exactly one of the sets.)
"""
new = intbitset()
new.bitset = other.bitset ^ self.bitset
return new

def symmetric_difference_update(self, other):
""" Update an intbitset with the symmetric difference of itself and another. """
self.bitset ^= other.bitset

def tolist(self):
"""
Legacy method to retrieve a list of all the elements inside an
intbitset.
"""
elements = []
for element in self:
elements = [element] + elements
return elements

def union(self, *args):
""" Return a new intbitset with elements from the intbitset and all others. """
new = intbitset()
bitset = self.bitset
for other in args:
bitset |= other.bitset
new.bitset = bitset
return new

def union_update(self, *args):
""" Update the intbitset, adding elements from all others. """
for other in args:
self.bitset |= other.bitset

def intersection(self, *args):
""" Return a new intbitset with elements common to the intbitset and all others. """
new = intbitset()
bitset = self.bitset
for other in args:
bitset &= other.bitset
new.bitset = bitset
return new

def intersection_update(self, *args):
""" Update the intbitset, keeping only elements found in it and all others. """
for other in args:
self.bitset &= other.bitset

def __and__(self, other):
"""
Return the intersection of two intbitsets as a new set.
(i.e. all elements that are in both intbitsets.)
"""
new = intbitset()
new.bitset = self.bitset & other.bitset
return new

def __eq__(self, other):
""" Return self==value. """
return self.bitset == other.bitset

def __contains__(self, key):
""" Return key in self. """
key_bit = 1 << key
return key_bit & self.bitset == key_bit

def __len__(self):
""" Return len(self). """
binary = bin(self.bitset)
size = len(binary) - 2
return (size - 2, size - 1)[binary[-1] != '0']

def __iter__(self):
""" Implement iter(self). """
bits = bin(self.bitset)[2:]
size = len(bits) - 1
for bit in bits:
if bit == "1":
yield size
size -= 1

def __hash__(self):
return self.bitset

def __str__(self):
binary = bin(self.bitset)[2:]
n = len(binary)
ans = "intbitset(["
for char in binary:
if char == "1":
ans += str(n - 1)
if n > 0:
ans += ", "
n -= 1
ans = ans.rstrip(', ')
ans += "])"
return ans
2 changes: 0 additions & 2 deletions tests/README.rst

This file was deleted.

Loading