nectar.utils module

nectar.utils.addTzInfo(t: datetime | date | time | None, timezone_str: str = 'UTC') datetime | None

Returns a datetime object with tzinfo added Uses Python’s built-in timezone when possible

nectar.utils.assets_from_string(text: str) List[str]

Correctly split a string containing an asset pair.

Splits the string into two assets with the separator being on of the following: :, /, or -.

nectar.utils.construct_authorperm(*args: Any) str

Create a post identifier from comment/post object or arguments. Examples:

>>> from nectar.utils import construct_authorperm
>>> print(construct_authorperm('username', 'permlink'))
@username/permlink
>>> print(construct_authorperm({'author': 'username', 'permlink': 'permlink'}))
@username/permlink
nectar.utils.construct_authorpermvoter(*args: Any) str

Create a vote identifier from vote object or arguments. Examples:

>>> from nectar.utils import construct_authorpermvoter
>>> print(construct_authorpermvoter('username', 'permlink', 'voter'))
@username/permlink|voter
>>> print(construct_authorpermvoter({'author': 'username', 'permlink': 'permlink', 'voter': 'voter'}))
@username/permlink|voter
nectar.utils.create_new_password(length: int = 32) str

Creates a random password containing alphanumeric chars with at least 1 number and 1 upper and lower char

nectar.utils.create_yaml_header(comment: Dict[str, Any], json_metadata: Dict[str, Any] | None = None, reply_identifier: str | None = None) str

Create a YAML front-matter header string from post/comment data and metadata.

Builds a YAML block (string) beginning and ending with ‘—’ that includes selected fields when present: - title (quoted) - permlink - author - “authored by” (from json_metadata[“author”]) - description (quoted) - canonical_url - app - last_update (from comment[“last_update”] or comment[“updated”]) - max_accepted_payout - percent_hbd - community (added when json_metadata[“tags”] exists and comment[“category”] differs from the first tag) - tags (comma-separated list) - beneficiaries (comma-separated entries formatted as “account:XX.XX%”; weights are converted from parts-per-10000 to percent with two decimals) - reply_identifier

Parameters:
comment (dict): Source post/comment data. Expected keys used include

“title”, “permlink”, “author”, “last_update” or “updated”, “max_accepted_payout”, optional “percent_hbd”, optional “category”, and optional “beneficiaries” (list of {“account”: str, “weight”: int}).

json_metadata (dict, optional): Parsed JSON metadata; may contain “author”,

“description”, “canonical_url”, “app”, and “tags” (list of strings).

reply_identifier (str or None, optional): If provided, added as “reply_identifier”.

Returns:

str: The composed YAML front-matter block as a string.

nectar.utils.derive_beneficiaries(beneficiaries: str | List[str]) List[Dict[str, Any]]

Parse beneficiaries and return a normalized, merged list of unique accounts with weights in basis points.

Accepts a comma-separated string or list with items like “account:10”, “@account:10%”, or “account” (unknown percentage). Duplicate accounts are merged by summing their explicit percentages and any share of the remaining percentage allocated to unknown entries. Unknown entries are distributed equally across all unknown slots.

Returns a list of dicts sorted by account name: [{“account”: str, “weight”: int_basis_points}] where weight is expressed in basis points (e.g., 1000 == 10%).

Derive a permlink from a comment title (for root level comments) or the parent permlink and optionally the parent author (for replies).

nectar.utils.derive_tags(tags: str) List[str]
nectar.utils.findall_patch_hunks(body: str | None = None) List[Tuple]
nectar.utils.formatTime(t: float | datetime | date | time) str | None

Properly Format Time for permlinks

nectar.utils.formatTimeFromNow(secs: int = 0) str

Properly Format Time that is x seconds in the future

Parameters:

secs (int) – Seconds to go in the future (x>0) or the past (x<0)

Returns:

Properly formated time for Graphene (%Y-%m-%dT%H:%M:%S)

Return type:

str

nectar.utils.formatTimeString(t: str | datetime | date | time) str

Properly Format Time for permlinks

nectar.utils.formatTimedelta(td: timedelta) str

Format timedelta to String

nectar.utils.formatToTimeStamp(t: datetime | date | time | str) int

Returns a timestamp integer

Parameters:

t (datetime) – datetime object

Returns:

Timestamp as integer

nectar.utils.generate_password(import_password: str, wif: int = 1) str
nectar.utils.import_coldcard_wif(filename: str | Path) Tuple[str, str]

Reads a exported coldcard Wif text file and returns the WIF and used path

nectar.utils.import_custom_json(jsonid: str, json_data: Dict[str, Any]) Tuple[List[str], List[str]]

Returns a list of required authorities for a custom_json operation.

Returns the author and required posting authorities for a custom_json operation.

Args:

jsonid: The id of the custom json (not used but kept for compatibility) json_data: The data of the custom json

Returns:

tuple with required author and posting authorities

nectar.utils.import_pubkeys(import_pub: str | Path) Tuple[str, str, str, str]
nectar.utils.load_dirty_json(dirty_json: str) Dict[str, Any]
nectar.utils.make_patch(a: str, b: str) str
nectar.utils.parse_time(block_time: str) datetime

Take a string representation of time from the blockchain, and parse it into datetime object.

nectar.utils.remove_from_dict(obj: Any, keys: List[str] | None = None, keep_keys: bool = True) Dict[str, Any]

Prune a class or dictionary of all but keys (keep_keys=True). Prune a class or dictionary of specified keys.(keep_keys=False).

nectar.utils.reputation_to_score(rep: str | int) float

Converts the account reputation value into the reputation score

nectar.utils.resolve_authorperm(identifier: str) Tuple[str, str]

Parse an author/permlink identifier and return (author, permlink).

Accepts plain “author/permlink” or “@author/permlink”, site URLs containing “/@author/permlink”, and dtube-style URLs containing “#!/v/<author>/<permlink>”. Returns a 2-tuple of strings (author, permlink). Raises ValueError if the identifier cannot be parsed.

nectar.utils.resolve_authorpermvoter(identifier: str) Tuple[str, str, str]

Correctly split a string containing an authorpermvoter.

Splits the string into author and permlink with the following separator: / and |.

nectar.utils.resolve_root_identifier(url: str) Tuple[str, str]
nectar.utils.seperate_yaml_dict_from_body(content: str) Tuple[str, Dict[str, Any]]