How to use camel_to_snake_case method in localstack

Best Python code snippet using localstack_python

build_dbc_cpp_code.py

Source:build_dbc_cpp_code.py Github

copy

Full Screen

...49def _canonical(value):50 """Replace anything but 'a-z', 'A-Z' and '0-9' with '_'.51 """52 return re.sub(r'[^a-zA-Z0-9]', '_', value)53def camel_to_snake_case(value):54 value = re.sub(r'(.)([A-Z][a-z]+)', r'\1_\2', value)55 value = re.sub(r'(_+)', '_', value)56 value = re.sub(r'([a-z0-9])([A-Z])', r'\1_\2', value).lower()57 value = _canonical(value)58 return value59class build_dbc_cpp_wrap():60 def __init__(self, work_dir, out_dir, namespace_prefix):61 self.work_dir = work_dir62 self.out_dir = out_dir63 self.namespace_prefix = namespace_prefix64 self.dbc_pairs = []65 self.include_files = []66 self.struct_defines = []67 def walk_dbc_files(self):68 self.dbc_pairs = []69 for root, dirs, files in os.walk(self.work_dir, topdown=False):70 for name in files:71 if name.endswith(".dbc"):72 dbc = os.path.join(root, name)73 stem, suffix = os.path.splitext(name)74 nm = "{}_{}".format(self.namespace_prefix, stem)75 v = (nm, dbc)76 self.dbc_pairs.append(v)77 def run_can_tools(self):78 for namespace,dbcpath in self.dbc_pairs:79 sh = "cantools generate_c_source --database-name {} {}".format(namespace, dbcpath)80 val = os.system(sh)81 ret = "Ret:[{}] Exec:[{}]".format(val, sh)82 print(ret)83 def get_include_files(self):84 lines = ["#include <iostream>"]85 for namespace,dbcpath in self.dbc_pairs:86 inc = "#include \"{}.h\"".format(namespace)87 lines.append(inc)88 lines.append("")89 lines.append('#include \"can_frame_data.hpp\"')90 return '\n'.join(lines)91 def get_struct_defines(self):92 lines = []93 for namespace, dbcpath in self.dbc_pairs:94 db = cantools.database.load_file(dbcpath)95 96 for message in db.messages:97 message_name = message.name98 message_name = camel_to_snake_case(message.name)99 100 signal_names = []101 member_lines = []102 init_lines = []103 print_lines = []104 for signal in message.signals:105 signal_name = camel_to_snake_case(signal.name)106 member_line = 'double {};'.format(signal_name.upper())107 init_line = '{} = 0;'.format(signal_name.upper())108 print_line = '<< \" {0}:\" << {0} << std::endl'.format(signal_name.upper())109 member_lines.append(member_line)110 init_lines.append(init_line)111 print_lines.append(print_line)112 signal_names.append(signal_name)113 struct_name_txt = 'T_{}'.format(message_name.upper())114 member_txt = '\n'.join(member_lines)115 fun_init_txt = '\n'.join(init_lines)116 fun_print_txt = '\n'.join(print_lines)117 line = STRUCT_DEFINE_FMT.format(struct_name=struct_name_txt,member=member_txt,fun_init=fun_init_txt,fun_print=fun_print_txt) 118 lines.append(line)119 return '\n'.join(lines)120 def get_struct_parses(self):121 lines = []122 for namespace, dbcpath in self.dbc_pairs:123 db = cantools.database.load_file(dbcpath) 124 125 for message in db.messages:126 message_name = message.name127 message_name = camel_to_snake_case(message.name) 128 129 parse_lines = []130 for signal in message.signals:131 signal_name = camel_to_snake_case(signal.name) 132 133 parse_line = 'data.{0} = {1}_{2}_{3}_decode(tmp.{3});'.format(signal_name.upper(), namespace, message_name.lower(), signal_name.lower()) 134 parse_lines.append(parse_line)135 136 137 fun_parse_txt = '\n'.join(parse_lines)138 139 line = STRUCT_PARSE_FMT.format( 140 space_name=namespace, 141 space_name_upper=namespace.upper(),142 message_name_lower=message_name.lower(),143 message_name_upper=message_name.upper(), 144 fun_parse=fun_parse_txt) 145 lines.append(line)146 return '\n'.join(lines)147 def get_struct_unpacks(self):148 lines = []149 for namespace, dbcpath in self.dbc_pairs:150 db = cantools.database.load_file(dbcpath) 151 152 unpack_messages = []153 for message in db.messages:154 message_name = message.name155 message_name = camel_to_snake_case(message.name) 156 unpack_message = '''157 T_{message_name_upper} _{message_name_lower}; 158 ret = PARSE_{message_name_upper}(can, _{message_name_lower});159 if(0==ret){{160 if (to_print) {{161 _{message_name_lower}.print();162 }} 163 }} else if (1==ret) {{164 }} else if (2==ret) {{165 std::cout << "struct T_{message_name_upper} data parse error." << std::endl; 166 }} 167 '''.format(message_name_lower=message_name.lower(), message_name_upper=message_name.upper())168 unpack_messages.append(unpack_message)169 line = STRUCT_UNPACK_FMT.format(space_name_upper=namespace.upper(), fun_unpack='\n'.join(unpack_messages))...

Full Screen

Full Screen

test_string.py

Source:test_string.py Github

copy

Full Screen

1from hutoolpy.string import camel_to_snake_case, snake_to_camel_case2def test_camel_to_snake_case():3 assert "a" == camel_to_snake_case("A")4 assert "a_b_c" == camel_to_snake_case("ABC")5 assert "abc" == camel_to_snake_case("ABC", False)6 assert "ab_c" == camel_to_snake_case("AbC")7 assert "a_bc" == camel_to_snake_case("ABc")8 assert "ab_cd" == camel_to_snake_case("AbCd")9 assert "sub_third_key2" == camel_to_snake_case("subThirdKey2")10def test_snake_to_camel_case():11 assert snake_to_camel_case("a") == "a"12 assert snake_to_camel_case("a_b_c") == "aBC"13 assert snake_to_camel_case("ab_c") == "abC"14 assert snake_to_camel_case("a_bc") == "aBc"15 assert snake_to_camel_case("ab_cd") == "abCd"...

Full Screen

Full Screen

Task#1.py

Source:Task#1.py Github

copy

Full Screen

1#!/usr/bin/env python32import re3def camel_to_snake_case(name):4 5 """6 >>> camel_to_snake_case('camelCaseNameAllowed')7 'camel_case_name_allowed'8 >>> camel_to_snake_case('longVATNumber')9 'long_vat_number'10 """11 inbetween = re.compile(r'''12 (13 (?<!^)[A-Z](?=[a-z])14 |15 (?<!^)(?<=[a-z])[A-Z]16 )17 ''', re.VERBOSE)18 return inbetween.sub(r'_\1', name).lower()19if __name__ == "__main__":20 import doctest...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful