Skip to main content

sc_crawler.utils

Utility functions, e.g. for hashing, database engines, and looking up tables and rows.

Classes:

Functions:

  • jsoned_hash – Hash the JSON-dump of all positional and keyword arguments.
  • sc_json_serializer – JSONJSON: JavaScript Object NotationA text format for structured data, built from objects (key-value pairs), arrays, strings, numbers, booleans, and null. Most languages and tools read and write it, which makes it a common format for API responses and benchmark output.JSONGlossary serializer for SQLAlchemy engines writing Crawler JSON columns.
  • create_sc_engine – Create a SQLAlchemy engine that can persist typed JSON model fields.
  • hash_database – Hash the content of a database.
  • chunk_list – Split a list into chunks of a specified size.
  • scmodels_to_dict – Creates a dict indexed by key(s) of the ScModels of the list.
  • is_sqlite – Checks if a SQLModel session is binded to a SQLite database.
  • is_postgresql – Checks if a SQLModel session is binded to a PostgreSQL-like database.
  • float_inf_to_str – Transform to string if a float is inf.
  • table_name_to_model – Return the ScModel schema for a table name.
  • get_row_by_pk – Get a row from a table definition by primary keys.
  • nesteddefaultdict – Recursive defaultdict.
  • list_search – Search for a dict in a list with the given key/value pair.
  • convert_gb_to_mib – Convert gigabytes to mebibytes.

func jsoned_hash utils.py:31–44⁠ (opens in a new tab)​

jsoned_hash(*args, **kwargs)

Hash the JSON-dump of all positional and keyword arguments.

Examples:

>>> jsoned_hash(42)
'0211c62419aece235ba19582d3cf7fd8e25f837c'
>>> jsoned_hash(everything=42)
'8f8a7fcade8cb632b856f46fc64c1725ee387617'
>>> jsoned_hash(42, 42, everything=42)
'f04a77f000d85929b13de04b436c60a1272dfbf5'

class HashLevels utils.py:47–52⁠ (opens in a new tab)​

Bases: Enum⁠ (opens in a new tab)

Granularity of sc_crawler.utils.hash_database: one hash for the database, or one per table or row.

func sc_json_serializer utils.py:55–57⁠ (opens in a new tab)​

sc_json_serializer(x)

JSON serializer for SQLAlchemy engines writing Crawler JSON columns.

func create_sc_engine utils.py:60–63⁠ (opens in a new tab)​

create_sc_engine(connection_string, **kwargs)

Create a SQLAlchemy engine that can persist typed JSON model fields.

func hash_database utils.py:66–108⁠ (opens in a new tab)​

hash_database(connection_string, level=HashLevels.DATABASE, ignored=['observed_at'], progress=None, exclude_tables=[])

Hash the content of a database.

Parameters:

Returns:

func chunk_list utils.py:111–119⁠ (opens in a new tab)​

chunk_list(items, size)

Split a list into chunks of a specified size.

Examples:

>>> [len(x) for x in chunk_list(range(10), 3)]
[3, 3, 3, 1]

func scmodels_to_dict utils.py:122–147⁠ (opens in a new tab)​

scmodels_to_dict(scmodels, keys)

Creates a dict indexed by key(s) of the ScModels of the list.

When multiple keys are provided, each ScModel instance will be stored in the dict with all keys. If a key is a list, then each list element is considered (not recursively, only at first level) as a key. Conflict of keys is not checked.

Parameters:

Examples:

>>> from sc_crawler.vendors import aws
>>> scmodels_to_dict([aws], keys=["vendor_id", "name"])
{'aws': Vendor...

func is_sqlite utils.py:150–152⁠ (opens in a new tab)​

is_sqlite(session)

Checks if a SQLModel session is binded to a SQLite database.

func is_postgresql utils.py:155–159⁠ (opens in a new tab)​

is_postgresql(session)

Checks if a SQLModel session is binded to a PostgreSQL-like database.

Dialect name is checked for PostgreSQLPostgreSQLAn open-source object-relational database system, developed by a global community and descended from the POSTGRES project at UC Berkeley in 1986. It uses and extends SQL, with transactions, many index types, extensions, and JSON data.PostgreSQL docsGlossary or CockroachDB.

func float_inf_to_str utils.py:162–164⁠ (opens in a new tab)​

float_inf_to_str(x)

Transform to string if a float is inf.

func table_name_to_model utils.py:167–174⁠ (opens in a new tab)​

table_name_to_model(table_name)

Return the ScModel schema for a table name.

func get_row_by_pk utils.py:177–191⁠ (opens in a new tab)​

get_row_by_pk(session, model, pks)

Get a row from a table definition by primary keys.

Parameters:

  • session (Session) – Connection for database connections.
  • model (ScModel) – An ScModel schema definition with table reference.
  • pks (dict⁠ (opens in a new tab)) – Dictionary of all the primary keys for the row,.

Returns:

  • ScModel – ScModel object read from the database.

func nesteddefaultdict utils.py:194–204⁠ (opens in a new tab)​

nesteddefaultdict()

Recursive defaultdict.

Examples:

>>> foo = nesteddefaultdict()
>>> foo["bar"]["baz"] = 43
>>> from json import dumps
>>> dumps(foo)
'{"bar": {"baz": 43}}'
list_search(items, key, values)

Search for a dict in a list with the given key/value pair.

When multiple values are provided, it will use the first field with a matching name with either keys.

func convert_gb_to_mib utils.py:218–227⁠ (opens in a new tab)​

convert_gb_to_mib(gb)

Convert gigabytes to mebibytes.

Parameters:

Returns: