# Python magic
## reading and writing textfiles
```python
with open(filename,'r') as txt:
txt.
```
* .readline() -> str: next line (upto and include newline) and return a string (including newline). It returns an empty string after EOF
* .readlines() -> [str]: all lines into list of strings
* .read() -> str: entire file into one string
```python
with open(filename,'w') as txt:
txt.write(aline\n)
```
* r : read-only - default
* w : write - erase all contents for existing file
* a : append
* r+ : read and write
## reading and writing csv files
```python
import csv
with open('my.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
for row in csv_reader:
#do something
with open('my.csv', mode='w') as csv_file:
csv_writer = csv.writer(csv_file, delimiter=',')
csv_writer.writerow(['John Smith', 'Accounting', 'November'])
#OR from dict
fieldnames = ['emp_name', 'dept', 'birth_month']
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writeheader()
writer.writerow({'emp_name': 'John Smith', 'dept': 'Accounting', 'birth_month': 'November'})
```
## argparse
Great tips also [here](https://realpython.com/command-line-interfaces-python-argparse/)
```python
parser = argparse.ArgumentParser()
parser.add_argument('--argname', dest='varname', default='default_content', help='some info text')
args = parser.parse_args()
print(args.varname)
```
## os
```python
import os
os.path.join(a,b,c)
os.path.split('somepathname/somefile.py')
os.path.splitext('somepath/somefile.py')
os.path.exists('somepath/semefile.py')
os.listdir(somepath) # only contents without path
```
-> see glob, pathlib
## glob
* to solve os.listdir issue
```python
import glob
```
## pathlib
(since python 3.4)
[docs](https://docs.python.org/3/library/pathlib.html)
```python
from pathlib import Path
p = Path('/my/awesome/path/setup.py')
p.name #'setup.py'
p.suffix #'.py'
p.root #'/'
p.parts # ('/', 'home', 'antoine', 'pathlib', 'setup.py')
p.relative_to('/home/antoine') #PosixPath('pathlib/setup.py')
p.exists() #True
p.st_size #filesize
```
## .rstrip()
* remove trailing characters (whitespace, newlines)
## string formatting
```python
print('hello %s' % name)
```
* %s : string
* %d : integer
* %f : float
* %.[number of digits]f : float with fixed amount of siginifcant numbers
## assertion
```python
assert condition, message
```
-> stops script execution
## docstring
```python
def some_function(argument):
"""
function to do something
input:
- this argument
output:
- a file
"""
```
to be displayed by ```some_function.__doc__```
or ```help(some_function)```
## pickle
write to file
```python
with open('outfile', 'wb') as fp:
pickle.dump(itemlist, fp)
```
read from file
```python
with open ('outfile', 'rb') as fp:
itemlist = pickle.load(fp)
```
## multiprocessing
```python
import multiprocessing as mp
pool = mp.Pool(usable_number_of_cores)
with fiona.open(large_polygon_shapefile,'r') as s2shp:
for tile in s2shp:
pool.apply_async(funcs.write_splitted_shapefiles, args = (bounding_box_small_poly_shp, tile, results_directory, small_polygon_shapefile))
pool.close()
pool.join()
```
## Type hinting
```python
def hello_name(name: str) -> str:
return(f"Hello {name}")
```
input name: str
output -> str
## Dcostring
help(hello_name) text can be given by:
```hello_name.__doc__ = 'docstring here'```
OR
```python
def hello_name(name):
""" docstring here"""
print('Hello')
```
from https://realpython.com/documenting-python-code/
Class docstrings should contain the following information:
A brief summary of its purpose and behavior
Any public methods, along with a brief description
Any class properties (attributes)
Anything related to the interface for subclassers, if the class is intended to be subclassed
The class constructor parameters should be documented within the __init__ class method docstring. Individual methods should be documented using their individual docstrings. Class method docstrings should contain the following:
A brief description of what the method is and what it’s used for
Any arguments (both required and optional) that are passed including keyword arguments
Label any arguments that are considered optional or have a default value
Any side effects that occur when executing the method
Any exceptions that are raised
Any restrictions on when the method can be called
## Numpydoc
```
"""Gets and prints the spreadsheet's header columns
Parameters
----------
file_loc : str
The file location of the spreadsheet
print_cols : bool, optional
A flag used to print the columns to the console (default is False)
Returns
-------
list
a list of strings representing the header columns
"""
```
{"metaMigratedAt":"2023-06-15T18:18:02.142Z","metaMigratedFrom":"YAML","title":"Python magic","breaks":true,"contributors":"[{\"id\":\"06ea2ab1-3ae0-4ad3-8c63-3ae6b5ef2b4d\",\"add\":5352,\"del\":421}]"}