Skip to content

Commit b086bc6

Browse files
committed
duplicates python directory, renaming it ruby
1 parent 7ce20ac commit b086bc6

File tree

9 files changed

+239
-0
lines changed

9 files changed

+239
-0
lines changed

ruby/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Python Naming Convention
2+
3+
The style guide for Python is based on [Guido’s ](https://www.python.org/doc/essays/styleguide/)naming convention recommendations.
4+
5+
List of covered sections:
6+
7+
* [Class Naming](../python/class-naming.md)
8+
* [Constant Naming](../python/constant-naming.md)
9+
* [Method Naming](../python/method-naming.md)
10+
* [Module Naming](../python/module-naming.md)
11+
* [Variable Naming](../python/variable-naming.md)
12+
* [Package Naming](../python/package-naming.md "Python Package Naming")
13+
* [Exception Naming](../python/exception-naming.md)
14+
* [Underscore](../python/underscore.md)
15+
16+
#### TL;DR
17+
18+
| Type | Public | Internal |
19+
| :--- | :--- | :--- |
20+
| Packages | `lower_with_under` | |
21+
| Modules | `lower_with_under` | `_lower_with_under` |
22+
| Classes | `CapWords` | `_CapWords` |
23+
| Exceptions | `CapWords` | |
24+
| Functions | `lower_with_under()` | `_lower_with_under()` |
25+
| Global/Class Constants | `CAPS_WITH_UNDER` | `_CAPS_WITH_UNDER` |
26+
| Global/Class Variables | `lower_with_under` | `_lower_with_under` |
27+
| Instance Variables | `lower_with_under` | `_lower_with_under` |
28+
| Method Names | `lower_with_under()` | `_lower_with_under()` |
29+
| Function/Method Parameters | `lower_with_under` | |
30+
| Local Variables | `lower_with_under` | |
31+
32+
Missing something? Please contribute here by reading [this guide](../docs/CONTRIBUTING.md).
33+

ruby/class-naming.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Python Naming Convention > Class Naming
2+
3+
## PascalCase
4+
5+
* Begin with an uppercase letter
6+
* Preferably a noun e.g. Car, Bird, MountainBike
7+
* Avoid acronyms and abbreviations
8+
9+
```python
10+
class Car:
11+
...
12+
```
13+
14+
## Private Class
15+
16+
Python does not support privacy directly. This naming convention is used as a weak internal use indicator.
17+
18+
* Should follow the above naming conventions
19+
* Should use a leading underscore (_) to distinguish between "public" and "private" classes
20+
* For more read the [official python documentation](https://docs.python.org/2/tutorial/classes.html#private-variables-and-class-local-references).
21+
22+
```
23+
class _Car: # private class
24+
```
25+
26+
27+

ruby/constant-naming.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Python Naming Convention > Constant Naming
2+
3+
## SCREAMING_SNAKE_CASE
4+
5+
* Should be all uppercase letters e.g. AGE, HEIGHT
6+
* If the name contains multiple words, it should be separated by underscores (_) such as DAYS_IN_MONTH
7+
* May contain digits but not as the first letter
8+
9+
```python
10+
class Product:
11+
MAX_TEMPERATURE = 36;
12+
```
13+
14+
15+

ruby/exception-naming.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Python Naming Convention > Exception Naming
2+
3+
## PascalCase
4+
5+
* Exceptions should be classes and hence the class naming convention should be used.
6+
* Add a suffix "Error" on your exception names, provided the exception is actually an error.
7+
* Avoid acronyms and abbreviations
8+
9+
```python
10+
class ElectricCarError(Exception):
11+
"""Basic exception for errors raised by non electric cars"""
12+
def __init__(self, car, type):
13+
if type is not "electric":
14+
msg = "%s is not an electric car" % car
15+
super(ElectricCarError, self).__init__(msg)
16+
self.car = car
17+
```
18+
19+
20+

ruby/file-naming.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Python module Convention > Package Naming
2+
3+
## snake_case
4+
5+
* Should be in lowercase.
6+
* If the name contains multiple words, an underscore (_) should separate it.E.g. expression_engine
7+
* The name should resonate with the class or methods inside the module
8+
9+
```python
10+
from math import factorial
11+
class Car:
12+
...
13+
```
14+
15+
16+

ruby/method-naming.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Python Naming Convention > Method Naming
2+
3+
## snake_case
4+
5+
* Should be all in lowercase
6+
* Preferably a verb e.g. get_car(), purchase(), book()
7+
* If the name contains multiple words, it should be separated by underscores \(\_\) e.g. get\_json\(\)
8+
9+
```python
10+
class Person:
11+
def get_height(self):
12+
return self.height
13+
```
14+
15+
## Private Method
16+
17+
Python does not support privacy directly. This naming convention is used as a weak internal use indicator.
18+
19+
* Should follow the above naming conventions
20+
* Should use a leading underscore (_) to distinguish between "public" and "private" functions in a module
21+
* For more read the [official python documentation](https://docs.python.org/2/tutorial/classes.html#private-variables-and-class-local-references).
22+
23+
```
24+
class Person:
25+
def _foot_to_centimeter(self): # private method
26+
return self.height * 30.48
27+
```
28+
29+
30+

ruby/module-naming.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Python module Convention > Module Naming
2+
3+
## snake_case
4+
- Should be all in lowercase letters such as requests, math
5+
- If contains multiple words, it should be separated by underscores (_) e.g. expression_engine.py
6+
- Should resonate with the class or methods inside the module
7+
8+
```python
9+
from math import factorial
10+
class Car:
11+
...
12+
```

ruby/underscore.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
The underscore (_) has special meaning in Python.
2+
3+
### For ignoring values:
4+
5+
If you do not need a specific value(s) while unpacking an object, just assign the value(s) to an underscore.
6+
7+
```
8+
x, _, y = (1, 2, 3)
9+
10+
# Ignore the multiple values. It is called "Extended Unpacking" which is available in only Python 3.x
11+
x, *_, y = (1, 2, 3, 4, 5) # x = 1, y = 5
12+
13+
# ignore the index
14+
for _ in range(10):
15+
task()
16+
17+
# Ignore a value of specific location
18+
for _, val in list_of_tuples: # [(1,2),(3,4),(5,6)]
19+
print(val) # output - 3
20+
```
21+
22+
### _single_leading_underscore
23+
24+
This convention is used for declaring private variables, functions, methods and classes. Anything with this convention are ignored in `from module import *`.
25+
26+
### single_trailing_underscore_
27+
28+
This convention should be used for avoiding conflict with Python keywords or built-ins.
29+
30+
```
31+
class_ = dict(n=50, boys=25, girls=25)
32+
# avoiding clash with the class keyword
33+
```
34+
35+
### __double_leading_underscore
36+
37+
Double underscore will mangle the attribute names of a class to avoid conflicts of attribute names between classes. Python will automatically add "\_ClassName" to the front of the attribute name which has a double underscore in front of it.
38+
39+
[Read more](https://docs.python.org/3/tutorial/classes.html#private-variables)
40+
41+
### __double_leading_and_trailing_underscore\_\_
42+
43+
This convention is used for special variables or ( magic )methods such as__init_\_, __len\__. These methods provides special syntactic features.
44+
45+
```
46+
class FileObject:
47+
'''Wrapper for file objects to make sure the file gets closed on deletion.'''
48+
49+
def __init__(self, filepath='~', filename='sample.txt'):
50+
# open a file filename in filepath in read and write mode
51+
self.file = open(join(filepath, filename), 'r+')
52+
```
53+
54+
[List of magic methods](https://github.com/RafeKettler/magicmethods/blob/master/magicmethods.pdf).
55+

ruby/variable-naming.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Python Naming Convention > Variable Naming
2+
3+
## snake_case
4+
5+
* Should be all in lowercase
6+
* Not begin with the special characters like e.g. & (ampersand), $ (dollar)
7+
* If the name contains multiple words, it should be separated by underscores (_) e.g. json_string
8+
* Avoid one-character variables e.g. a, b
9+
10+
```python
11+
class Student
12+
age = None
13+
...
14+
```
15+
16+
## Private Variable
17+
18+
Python does not support privacy directly. This naming convention is used as a weak internal use indicator.
19+
20+
* Should follow the above naming conventions
21+
* Should use a leading underscore (_) to distinguish between "public" and "private" variables
22+
* For more read the [official python documentation](https://docs.python.org/2/tutorial/classes.html#private-variables-and-class-local-references).
23+
24+
```
25+
class Student:
26+
age = None # public variable
27+
_id = 0 # Private variable
28+
```
29+
30+
31+

0 commit comments

Comments
 (0)