What is the naming convention in Python for variables and functions?

See Python PEP 8: Function and Variable Names: Function names should be lowercase, with words separated by underscores as necessary to improve readability. Variable names follow the same convention as function names. mixedCase is allowed only in contexts where that’s already the prevailing style (e.g. threading.py), to retain backwards compatibility.

Regex for PascalCased words (aka camelCased with leading uppercase letter)

([A-Z][a-z0-9]+)+ Assuming English. Use appropriate character classes if you want it internationalizable. This will match words such as “This”. If you want to only match words with at least two capitals, just use ([A-Z][a-z0-9]+){2,} UPDATE: As I mentioned in a comment, a better version is: [A-Z]([A-Z0-9]*[a-z][a-z0-9]*[A-Z]|[a-z0-9]*[A-Z][A-Z0-9]*[a-z])[A-Za-z0-9]* It matches strings that start with an uppercase letter, … Read more

Force CamelCase on ASP.NET WebAPI Per Controller

Thanks to @KiranChalla I was able to achieve this easier than I thought. Here is the pretty simple class I created: using System; using System.Linq; using System.Web.Http.Controllers; using System.Net.Http.Formatting; using Newtonsoft.Json.Serialization; public class CamelCaseControllerConfigAttribute : Attribute, IControllerConfiguration { public void Initialize(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor) { var formatter = controllerSettings.Formatters.OfType<JsonMediaTypeFormatter>().Single(); controllerSettings.Formatters.Remove(formatter); formatter = new JsonMediaTypeFormatter { … Read more

Acronyms in CamelCase [closed]

There are legitimate criticisms of the Microsoft advice from the accepted answer. Inconsistent treatment of acronyms/initialisms depending on number of characters: playerID vs playerId vs playerIdentifier. The question of whether two-letter acronyms should still be capitalized if they appear at the start of the identifier: USTaxes vs usTaxes Difficulty in distinguishing multiple acronyms: i.e. USID … Read more

How can I return camelCase JSON serialized by JSON.NET from ASP.NET MVC controller methods?

or, simply put: JsonConvert.SerializeObject( <YOUR OBJECT>, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }); For instance: return new ContentResult { ContentType = “application/json”, Content = JsonConvert.SerializeObject(new { content = result, rows = dto }, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }), ContentEncoding = Encoding.UTF8 };

JSON Naming Convention (snake_case, camelCase or PascalCase) [closed]

In this document Google JSON Style Guide (recommendations for building JSON APIs at Google), It recommends that: Property names must be camelCased, ASCII strings. The first character must be a letter, an underscore (_), or a dollar sign ($). Example: { “thisPropertyIsAnIdentifier”: “identifier value” } My team consistently follows this convention when building REST APIs. … Read more

RegEx to split camelCase or TitleCase (advanced)

The following regex works for all of the above examples: public static void main(String[] args) { for (String w : “camelValue”.split(“(?<!(^|[A-Z]))(?=[A-Z])|(?<!^)(?=[A-Z][a-z])”)) { System.out.println(w); } } It works by forcing the negative lookbehind to not only ignore matches at the start of the string, but to also ignore matches where a capital letter is preceded by … Read more

Converting any string into camel case

Looking at your code, you can achieve it with only two replace calls: function camelize(str) { return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function(word, index) { return index === 0 ? word.toLowerCase() : word.toUpperCase(); }).replace(/\s+/g, ”); } camelize(“EquipmentClass name”); camelize(“Equipment className”); camelize(“equipment class name”); camelize(“Equipment Class Name”); // all output “equipmentClassName” Edit: Or in with a single replace call, capturing … Read more

Elegant Python function to convert CamelCase to snake_case?

Camel case to snake case import re name=”CamelCaseName” name = re.sub(r'(?<!^)(?=[A-Z])’, ‘_’, name).lower() print(name) # camel_case_name If you do this many times and the above is slow, compile the regex beforehand: pattern = re.compile(r'(?<!^)(?=[A-Z])’) name = pattern.sub(‘_’, name).lower() To handle more advanced cases specially (this is not reversible anymore): def camel_to_snake(name): name = re.sub(‘(.)([A-Z][a-z]+)’, r’\1_\2′, … Read more