backtrader.utils.py3 module

Python 3 Compatibility Module.

This module provides common type aliases and utility functions used throughout the backtrader framework. Originally a Python 2/3 shim, now simplified to Python 3 only.

Exports:

string_types: Tuple of string types — (str,). integer_types: Tuple of integer types — (int,). MAXINT / MININT / MAXFLOAT / MINFLOAT: Numeric limits. range, zip, map, filter: Built-in references (kept for import compat). queue: import queue. cmp, bytes, bstr: Helper functions. iterkeys, itervalues, iteritems, keys, values, items: Dict helpers. urlquote, urlopen, ProxyHandler, build_opener, install_opener: URL helpers. winreg: Windows registry module (None on non-Windows). with_metaclass: Metaclass helper.

backtrader.utils.py3.urlquote(s, *args, **kwargs)[source]

Quote a string for use in a URL.

Parameters:
  • s – The string to quote.

  • *args – Additional positional arguments passed to urllib.parse.quote.

  • **kwargs – Additional keyword arguments passed to urllib.parse.quote.

Returns:

The quoted string safe for use in URLs.

backtrader.utils.py3.urlopen(*args, **kwargs)[source]

Open a URL.

Parameters:
  • *args – Positional arguments passed to urllib.request.urlopen.

  • **kwargs – Keyword arguments passed to urllib.request.urlopen.

Returns:

A file-like object representing the URL response.

backtrader.utils.py3.ProxyHandler(*args, **kwargs)[source]

Create a proxy handler for opening URLs.

Parameters:
  • *args – Positional arguments passed to urllib.request.ProxyHandler.

  • **kwargs – Keyword arguments passed to urllib.request.ProxyHandler.

Returns:

A ProxyHandler instance for configuring URL proxies.

backtrader.utils.py3.build_opener(*args, **kwargs)[source]

Build a URL opener with a chain of handlers.

Parameters:
  • *args – Positional arguments passed to urllib.request.build_opener.

  • **kwargs – Keyword arguments passed to urllib.request.build_opener.

Returns:

An OpenerDirector instance configured with the specified handlers.

backtrader.utils.py3.install_opener(*args, **kwargs)[source]

Install an opener as the default global opener.

Parameters:
  • *args – Positional arguments passed to urllib.request.install_opener.

  • **kwargs – Keyword arguments passed to urllib.request.install_opener.

Returns:

None.

backtrader.utils.py3.cmp(a, b)[source]

Compare two values.

Parameters:
  • a – First value to compare.

  • b – Second value to compare.

Returns:

1 if a > b, 0 if a == b, -1 if a < b.

Return type:

int

backtrader.utils.py3.bytes(x)[source]

Encode a string to bytes using UTF-8 encoding.

Parameters:

x – String to encode.

Returns:

Bytes representation of the input string.

backtrader.utils.py3.bstr(x)[source]

Convert a value to a byte string.

Parameters:

x – Value to convert.

Returns:

String representation of the input value.

backtrader.utils.py3.iterkeys(d)[source]

Return an iterator over the dictionary’s keys.

Parameters:

d – Dictionary to iterate over.

Returns:

An iterator over the dictionary’s keys.

backtrader.utils.py3.itervalues(d)[source]

Return an iterator over the dictionary’s values.

Parameters:

d – Dictionary to iterate over.

Returns:

An iterator over the dictionary’s values.

backtrader.utils.py3.iteritems(d)[source]

Return an iterator over the dictionary’s items.

Parameters:

d – Dictionary to iterate over.

Returns:

An iterator over (key, value) tuples.

backtrader.utils.py3.keys(d)[source]

Return a list of the dictionary’s keys.

Parameters:

d – Dictionary to extract keys from.

Returns:

A list containing the dictionary’s keys.

backtrader.utils.py3.values(d)[source]

Return a list of the dictionary’s values.

Parameters:

d – Dictionary to extract values from.

Returns:

A list containing the dictionary’s values.

backtrader.utils.py3.items(d)[source]

Return a list of the dictionary’s items.

Parameters:

d – Dictionary to extract items from.

Returns:

A list of (key, value) tuples.

backtrader.utils.py3.with_metaclass(meta, *bases)[source]

Create a base class with a metaclass.

This function provides a compatibility layer for creating classes with metaclasses in a way that works across Python versions. Originally designed for Python 2/3 compatibility, now simplified for Python 3 only.

Parameters:
  • meta – The metaclass to use for the created class.

  • *bases – Base classes to inherit from.

Returns:

A temporary base class that, when inherited from, creates a class with the specified metaclass and base classes.

Note

Modern Python 3 code can use metaclass directly in class definition: class MyClass(metaclass=Meta):. This function is kept for backward compatibility with existing code.