MCPcopy Create free account
hub / github.com/sammchardy/python-binance / convert_to_function_name

Function convert_to_function_name

code-generator.py:253–322  ·  view source on GitHub ↗

Convert an endpoint path to a consistent function name format with appropriate prefix and version. Examples: GET, /api/v3/ticker/tradingDay -> v3_get_ticker_trading_day GET, /sapi/v1/margin/order -> margin_v1_get_order GET, /fapi/v1/ticker/price -> futures_v1_get_tic

(method: str, endpoint: str)

Source from the content-addressed store, hash-verified

251
252
253def convert_to_function_name(method: str, endpoint: str) -> str:
254 """
255 Convert an endpoint path to a consistent function name format with appropriate prefix and version.
256 Examples:
257 GET, /api/v3/ticker/tradingDay -> v3_get_ticker_trading_day
258 GET, /sapi/v1/margin/order -> margin_v1_get_order
259 GET, /fapi/v1/ticker/price -> futures_v1_get_ticker_price
260 GET, /dapi/v1/ticker/price -> futures_coin_v1_get_ticker_price
261 GET, /vapi/v1/ticker -> options_v1_get_ticker
262 """
263 # Get the request function and path info
264 request_function, cleaned_endpoint, version = get_request_function_and_path(
265 endpoint
266 )
267
268 # Map request functions to their prefix in the function name
269 PREFIX_NAME_MAP = {
270 "_request_margin_api": "margin",
271 "_request_papi_api": "papi",
272 "_request_futures_api": "futures",
273 "_request_futures_coin_api": "futures_coin",
274 "_request_options_api": "options",
275 }
276
277 # Remove known prefixes and version segments first
278 cleaned_endpoint = endpoint
279 sorted_prefixes = sorted(PREFIX_MAP.keys(), key=len, reverse=True)
280 for prefix in sorted_prefixes:
281 if cleaned_endpoint.startswith(prefix):
282 cleaned_endpoint = cleaned_endpoint[len(prefix) :]
283 break
284
285 # Remove version segments and leading/trailing slashes
286 cleaned_endpoint = re.sub(r"/v\d+/", "/", cleaned_endpoint)
287 cleaned_endpoint = cleaned_endpoint.strip("/")
288
289 # Split on slashes and process each part
290 parts = cleaned_endpoint.split("/")
291 processed_parts = []
292
293 for part in parts:
294 # Replace hyphens with underscores
295 part = part.replace("-", "_")
296 part = part.replace(".", "_")
297
298 # Insert underscore before capital letters in camelCase
299 part = re.sub(r"([a-z0-9])([A-Z])", r"\1_\2", part)
300
301 # Convert to lowercase
302 part = part.lower()
303
304 # Remove any duplicate underscores
305 part = re.sub(r"_+", "_", part)
306
307 processed_parts.append(part)
308
309 # Join all parts with underscores
310 base_name = "_".join(processed_parts)

Callers 2

generate_function_codeFunction · 0.85

Calls 1

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…