CamelCase to snake_case: python

Ebates Coupons and Cash Back


Given a value that’s in CamelCase (like a Java Variable Name pattern), and you want to change it to snake_case (python, perl, and maybe other scripting languages), here is the python function to do it.

def to_snake_case(given_value):
    import re
    s = re.compile('([A-Z])')
    return s.sub(r'_\1', given_value).lower().replace('__', '_').lstrip('_')

print(to_snake_case1(‘CamelCase’))
print(to_snake_case1(‘camelcase’))
print(to_snake_case1(‘CCamAAelCase’))
print(to_snake_case1(‘CCamel_Case’))

camel_case
camelcase
c_cam_a_ael_case
c_camel_case

If you want the Consecutive Capital Letters to be kept as one group, then a slight modification of the function will do.

def to_snake_case1(given_value):
    import re
    s = re.compile('([A-Z]+)')
    return s.sub(r'_\1', given_value).lower().replace('__', '_').lstrip('_')

print(to_snake_case1(‘CamelCase’))
print(to_snake_case1(‘camelcase’))
print(to_snake_case1(‘CCamAAelCase’))
print(to_snake_case1(‘CCamel_Case’))

camel_case
camelcase
ccam_aael_case
ccamel_case

If you want to give a list/array of values, and return all the values changed to snake case, here’s the modified function.

def to_snake_case(given_list):
    import re
    s = re.compile('([A-Z]+)')
    result = []
    for e in given_list:
        result.append(b.sub(r'_\1', e).lower().replace('__', '_').lstrip('_'))
    return result

print(to_snake_case2([‘CamelCase’, ‘camelcase’, ‘CCamAAelCase’, ‘CCamel_Case’]))

[‘camel_case’, ‘camelcase’, ‘ccam_aael_case’, ‘ccamel_case’]


Ebates Coupons and Cash Back

 

Install 3rd party Python Modules

When you have both versions of Python (2 and 3), and have ‘pip’ installed already in python, you can install the 3rd party python modules (e.g, bitstring module) with the below commands.

Make sure you replace your module name in place of bitstring

Python 3:

  1. Open the Terminal and ‘cd’ (change directory) to the bin folder of where python 3 was installed.
  2. Run the command ‘pip3 install bitstring

Python 2:

  1. Open the Terminal
  2. Run the command ‘pip install bitstring’

You may want to add ‘sudo‘ in the beginning of the command like ‘sudo pip install bitstring‘ (and enter your system password) if the earlier command errors due to insufficient privileges.

Simple Script for Fibonacci Series using Python

#!/usr/bin/python3
#Script to find Fibonacci Series up to a certain maximum number (say 100 for e.g.,) in Python 3
def main():
    a, b = 0, 1
    print(0, end = ' ') #If you want the older style fibonacci series, you can comment out this line
    while b <= 100:
        print(b, end=' ')
        a, b = b, a + b
if __name__ == "__main__": main()
Same Script in Python 2 Version

#!/usr/bin/python
#Script to find Fibonacci Series up to a certain maximum number (say 100 for e.g.,) in Python 2
def main():
    a, b = 0, 1
    print 0, #If you want the older style fibonacci series, you can comment out this line
    while b <= 100:
        print b,
        a, b = b, a + b
if __name__ == "__main__": main()